试图将文件上传到服务器,但php脚本没有获取文件详细信息

时间:2012-05-03 13:53:35

标签: php html sql

我正在尝试将图像上传到服务器,然后获取该图像名称并将其插入到mysql数据库中。在我到达数据库位之前,上传不起作用。我把它缩小到$ _FILES数组中没有数据的事实,当我回显$ imageFileName是空白时。

以下是我表单的一部分:

<form name="addItem" method="post" action="add-new-item.php">   
    <input name="name" placeholder="Portfolio Item Name" type="text" id="itemName"/><br />
    <input type="file" name="imageName" id="imageName" /><br />

</form>

脚本,它是表单所在页面的一部分:

   $target = "images/";
   $target = $target . basename( $_FILES['imageName']['name']);        
   $imageFileName=($_FILES['imageName']['name']);

    if(move_uploaded_file($_FILES['imageName']['tmp_name'], $target)){

          echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }else {
          echo "Sorry, there was a problem uploading your file.";
    }

 $query1="INSERT INTO portfolio_items (item_name,full_size_image)  VALUES('$itemName','$itemImage')"; 

1 个答案:

答案 0 :(得分:4)

您需要在<form>代码中加入以下内容:

enctype="multipart/form-data"

所以它会变成:

<form name="addItem" method="post" action="add-new-item.php" enctype="multipart/form-data">   

Documented very well here - 请参阅第一页底部的说明...