使用mysql文件名更新的php文件上传

时间:2012-06-08 22:03:52

标签: php mysql upload

我正在尝试将这两个脚本(文件上传)和(一个mysql更新)结合起来,以便将图像文件上传到正确的文件夹,然后在mysql数据库中更新文件路径。我知道$ sql更新查询是错误的,这就是我的麻烦所在。任何帮助都会很棒。

//db connection
require "connect.db.php";

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

// update data in mysql database
$sql="UPDATE `characters` SET ch_image='/upload/$_FILES["file"]["name"]' WHERE ID='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.html'>View result</a>";
touch('../file.html');
clearstatcache();
}

else {
echo "Whoops: " . mysql_error(); ;
}
mysql_close();
?>

1 个答案:

答案 0 :(得分:1)

$sql更改为此

$sql="UPDATE `characters` SET ch_image='/upload/" . $_FILES['file']['name'] . "' WHERE ID='$id'";