将图像上传到服务器+将文件名插入到dB

时间:2011-11-17 09:58:50

标签: php upload

php:

//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

3 个答案:

答案 0 :(得分:0)

错误不必眩目代码 代码执行时引发的错误。
并且必须注意和调查这些错误。

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

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

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

答案 1 :(得分:0)

您编写(移动)图像的apache子目录必须是可写的,即具有写入文件的权限。因为它看起来像你在Linux CD上的目录和更改权限: cd / / parent / directory / to / directory / where / files / are / guess / to / be chmod 777 theimagefilesdir /

请务必阅读权限并选择一个适合您的权限,因为777非常宽松并且对于制作而言可能不切实际但我在本地计算机中使用它:)

答案 2 :(得分:-1)

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