2013-08-07 50 views
3

我有以下形式:

<form method="post" action="index.php"> 
    product name: 
    <input type="text" name="product_name" value="<?php echo $product_name;?>"/> 
    <br /> <br /> 
    product details 
    <textarea rows = "6" cols = "30" name="product_details" > <?php echo $product_details;?></textarea> 
    <br /> <br /> 
    product price 
    <input type="text" name = "product_price" value="<?php echo $product_price;?>"/> 
    <br /> <br /> 
    CN: 
    <input type="text" name = "product_cn" value="<?php echo $product_cn;?>"/> 
    <br /> <br /> 
    image 
    <input type="file" name="fileField" /> 
    <br /> <br /> 

    <input type="submit" name="submit" value="register product" /> 
</form> 

我的問題是,每當我試圖處理圖像使用此代碼:

move_uploaded_file($_FILES['fileField']['tmp_name'], "../product_images/$newname"); 

我得到以下錯誤:

Notice: Undefined index: fileField

這是爲什麼?

在此先感謝!

回答

5

您必須添加enctype='multipart/form-data'到窗體
Quote from this topic about this

When you make a POST request, you have to encode the data that forms the body of the request in some way.

HTML forms provide two methods of encoding. The default is application/x-www-form-urlencoded, which is more or less the same as a query string on the end of the URL. The other, multipart/form-data, is a more complicated encoding but one which allows entire files to be included in the data.

+0

謝謝!這是愚蠢的忘記那部分> _ < – kfirba

2
enctype="multipart/form-data" 

該屬性添加到您的表單標籤

<form method="post" action="index.php" enctype="multipart/form-data"> 

multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control

http://www.w3schools.com/tags/att_form_enctype.asp