2012-05-03 65 views
0

我想上傳一個圖像到服務器,然後獲取該圖像名稱,並將其插入到MySQL數據庫。在我到達數據庫位之前,上傳不起作用。我將其縮小到了$ _FILES數組中沒有數據的事實,當我回顯出$ imageFileName時它是空白的。試圖上傳文件到服務器,但PHP腳本沒有獲取文件的詳細信息

這裏是我的窗體的一部分:

<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')"; 

回答

4

您需要在您的<form>標籤以下內容:

enctype="multipart/form-data" 

因此,這將成爲:

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

Documented very well here - 看筆記的第一頁底部...

+0

我愛你,謝謝你。很簡單。 – Nicola

+0

@Nicola沒有probs ...毫無疑問,有一天你會用相同的答案回答這種類型的問題... – ManseUK

+0

是否需要打開數據庫連接才能工作? – Nicola

相關問題