上传图像到服务器+插入文件名到数据库


Uploading a image to a server + inserting filename to dB

//This is the directory where images will be saved
$target = "/images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$strListItemPic=(mysql_real_escape_string($_FILES['photo']['name']));
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " 
      has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
html:

    <input type="hidden" name="size" value="350000">
    <input type="file" name="photo">  

它爆发并得到"Sorry, there was a problem uploading your file.";

我稍后在MySQL语句中插入$strListItemPic,因此我可以在其他地方的变量中回显图片。

这段代码中有什么明显的错误吗?$target必须是绝对路径吗?

PHP错误:

Warning: move_uploaded_file(/xxxxxx/lists/images/test.gif): 
failed to open stream: No such file or directory in 
/home/virtual/site48/fst/var/www/html/xxxxxx/lists/itemedit.php on line 22

Warning: move_uploaded_file(): Unable to move '/tmp/phpJ3v7HV' to 
'/xxxxxx/lists/images/test.gif' 
in /home/virtual/site48/fst/var/www/html/xxxxxx/lists/itemedit.php on line 22
固定:

Added enctype="multipart/form-data" to  <form>
Removed / before images in $target

错误不必出现在代码中。
在执行代码时引发的错误。
这些错误必须被注意和调查。

至少将这两行添加到你的代码中并再次运行。

ini_set('display_errors',1);
error_reporting(E_ALL);

或以任何其他方式获取move_uploaded_file产生的错误信息。
试图在没有实际错误消息的情况下回答您的问题将是无用的浪费时间。

你写(移动)图像的apache子目录必须是可写的,即有写文件的权限。因为它看起来像你在Linux上cd到目录和更改权限使用:cd//父母/目录/文件/目录/,///应该/,/Chmod 777 theimagefile/

请务必阅读权限并选择适合您的权限,因为777是非常宽松的,在生产中可能不切实际,但我在本地机器中使用它:)

请检查您是否在表单标签中使用了enctype