将文件上传到服务器时,保存MySQL数据库中的文件路径

时间:2014-09-17 08:33:24

标签: php mysql

我是PHP的新手。

我编写了以下PHP代码,以两种尺寸重新调整图像大小,效果很好。

我的问题

我需要在MySQL数据库中保存上传的图像路径。

路径

1)小图片的路径

2)大图像的路径

这是代码

<?php 
error_reporting(0);

$change="";
$abc="";


 define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;


 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];


    if ($image) 
    {

        $filename = stripslashes($_FILES['file']['name']);

        $extension = getExtension($filename);
        $extension = strtolower($extension);


 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {

            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
        }
        else
        {

 $size=filesize($_FILES['file']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);


$newwidth=150;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);


$newwidth1=50;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


$filename = "profile.pic/profile/big" .date('Y-m-d_His - '). $_FILES['file']['name'];

$filename1 = "profile.pic/header/small".date('Y-m-d_His - '). $_FILES['file']['name'];



imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}

}

//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
 {

   // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
    $change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
 }

?>

这是表格

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
    <meta content="en-us" http-equiv="Content-Language">

        <title>picture demo</title>

       <link href="file:///C|/Users/Rameen/Downloads/.css" media="screen, projection" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>



    <style type="text/css">
      .help
    {
    font-size:11px; color:#006600;
    }
    body {
        color: #000000;
        background-color:#999999 ;
        background:#999999 url(<?php echo $user_row['img_src']; ?>) fixed repeat top left;
        font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; 
        }
    .msgdiv{
        width:759px;
        padding-top:8px;
        padding-bottom:8px;
        background-color: #fff;
        font-weight:bold;
        font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px;
        }
    #container{width:763px;margin:0 auto;padding:3px 0;text-align:left;position:relative; -moz-border-radius: 6px;-webkit-border-radius: 6px; background-color:#FFFFFF;}</style>

      </head><body>
         <div align="center" id="err">
    <?php echo $change; ?>  </div>
       <div id="space"></div>





      <div id="container" >

       <div id="con">



            <table width="502" cellpadding="0" cellspacing="0" id="main">
              <tbody>
                <tr>
                  <td width="500" height="238" valign="top" id="main_right">

                  <div id="posts">
                  &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename; ?>" />  &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename1; ?>"  />
                    <form method="post" action="" enctype="multipart/form-data" name="form1">
                    <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
                   <tr><Td style="height:25px">&nbsp;</Td></tr>
            <tr>
              <td width="150"><div align="right" class="titles">Picture 
                : </div></td>
              <td width="350" align="left">
                <div align="left">
                  <input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>

                  </div></td>  
            </tr>
            <tr><Td></Td>
            <Td valign="top" height="35px" class="help">Image maximum size <b>400 </b>kb</span></Td>
            </tr>
            <tr><Td></Td><Td valign="top" height="35px"><input type="submit" id="mybut" value="       Upload        " name="Submit"/></Td></tr>
            <tr>
              <td width="200">&nbsp;</td>
              <td width="200"><table width="200" border="0" cellspacing="0" cellpadding="0">    
                  <tr>
                    <td width="200" align="center"><div align="left"></div></td>
                    <td width="100">&nbsp;</td>
                  </tr>
              </table></td>
            </tr>
          </table>
                    </form>
          </div>
                  </td>            
                </tr>
              </tbody>
         </table>  
    </div>

      </div>
    </body></html>

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您应该为这两个图片尝试move_uploaded_file功能。在服务器中创建两个sepearte目录并遍历这两个文件并使用move_uploaded_file

还在数据库中创建两个字段以存储两个路径。并将两部分都放入其中。

答案 1 :(得分:0)

$路径=&#39;上传/&#39; $ _ FILES [&#39;文件&#39;] [&#39;名称&#39;];
 move_uploaded_file($ _ FILES [&#39; file&#39;] [&#39; tmp_name&#39;],$ path);
 mysql_query(&#34;插入tablename(文件路径)值(&#39; $ path&#39;)&#34;);

答案 2 :(得分:0)

问题:我需要在MySQL数据库中保存上传的图像路径

你已经有了目录,所以我认为需要为此编写一个数据库查询。

$yourDir = "path to your directory";
$completePathtoYourFile = $yourDir . "/" . $imageName . ".jpg";   //do not append .jpg if already.

了解如何连接到mysql数据库。 DB CONNECTION

$insert = "INSERT INTO table_name (field1, field2) VALUES ('$completePathtoYourFile', .......) ";
$result = mysqli_query($connectionVariable, $insert);
if(!$result){
   echo "Not Inserted..";
}
else{
   echo "Saved..";
}

正如其他人所提到的,您需要将上传的文件从临时存储移动到目标路径为 $ yourDir 的目录。