简体   繁体   中英

Image upload form not working when I use enctype=“multipart/form-data”

I am having issues trying to get this image upload to work. It isn't working whether I use enctype='multipart/form-data' or not but when i am not using it the page will show me my errors, when I add enctype='multipart/form-data' to the form tag it does nothing at all when I click submit. Does anyone see any problems? Any help is appreciated!

Below is the upload code:

//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}






    if ($submit == "Add" && $_SERVER['REQUEST_METHOD'] == 'POST' && $product_name != "" &&  $product_image != "" &&  $product_description != "" &&  $product_price != "" && $product_dimensions != "" &&  $product_category != "" && $product_subcategory != "" && $product_manufacturer != "" && $product_toronto_avail != "" && $product_mississauga_avail != "" && $product_concord_avail != "" && $product_pickering_avail != ""  && $product_barrie_avail != ""  && $product_kitchener_avail != "") {

            // Create Query
            $addQuery="INSERT INTO products (name, image, description, price, dimensions, category, subcategory, manufacturer, toronto, mississauga, concord, pickering, barrie, kitchener) VALUES ('$product_name', '$product_image', '$product_description', '$product_price', '$product_dimensions', '$product_category', '$product_subcategory', '$product_manufacturer', '$product_toronto_avail', '$product_mississauga_avail', '$product_concord_avail', '$product_pickering_avail', '$product_barrie_avail', '$product_kitchener_avail')";

            // Run Query & Add into products table
            mysql_query($addQuery);







//reads the name of the file the user submitted for uploading
$image= $product_image;
//if it is not empty
if ($image)
{
//get the extension of the file in a lower case format
$extension = getExtension($image);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($image);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="../gallery/".$image_name;
//we verify if the image has been uploaded, and print error instead
$moved = move_uploaded_file($image_name,$newname);
if (!$moved)
{
echo '<h1>Copy unsuccessfull!</h1>';
echo $newname . '<br/>';
echo $image_name;
}}}






            //Successful!
            $successAdd = "<p style='font-size:12px;font-style:italic;font-family:georgia;'>You have successfully added \"<span style='color:#014380;'>" . $product_name . "</span>\" to your products database.</p>";

    } else if ($submit == "Add" && $_SERVER['REQUEST_METHOD'] == 'POST' && $product_name == "" || $product_image == "" || $product_description == "" || $product_price == "" ||  $product_dimensions == "" || $product_category == "" || $product_subcategory == "" || $product_manufacturer == "" || $product_toronto_avail == "" || $product_mississauga_avail == "" || $product_concord_avail == "" || $product_pickering_avail == "" || $product_barrie_avail == "" || $product_kitchener_avail == "") {

            $successAdd = "<p style='font-size:12px;color:red;font-style:italic;font-family:georgia;'>Please fill in all required fields. </p>";

    } else {
        $successAdd = "";   
    }

& below is the form code:

echo "<form method='post' enctype='multipart/form-data' action='./add_product.php'  >";
                    echo "<table border='0' class='addProductTable' cellpadding='3' cellspacing='0'>";
                    print ("<tr class='tableHeader large'> 
                            <th style='width:200px;'></th> 
                            <th style='width:175px;'></th> 
                            <th></th> 
                          </tr>
                          <tr>
                            <td> Name:* </td>
                            <td> <input type='text' name='name'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Upload an Image:* </td>
                            <td> <input type='file' name='image'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Description:* </td>
                            <td colspan='2'> <textarea name='description' cols='25' rows='5'></textarea> </td>
                          </tr>
                          <tr>
                            <td> Price:* </td>
                            <td> <input type='text' name='price'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Dimensions:* </td>
                            <td> <input type='text' name='dimensions'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Category:* </td>
                            <td> <input type='text' name='category'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Subcategory:* </td>
                            <td> <input type='text' name='subcategory'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Manufacturer:* </td>
                            <td> <input type='text' name='manufacturer'  /> </td>
                            <td></td>
                          </tr>
                          <tr>
                            <td> Availability:* </td>
                            <td colspan='2'> 
                                <div style='float:left; margin-right:10px;'>
                                    <span>Toronto:</span> <input type='text' style='width:30px;' name='toronto_availability'  /> <br/>
                                    <span>Mississauga:</span> <input type='text' style='width:30px;' name='mississauga_availability'  /> <br/>
                                    <span>Concord:</span> <input type='text' style='width:30px;' name='concord_availability'  /> <br/>
                                </div>
                                <div style='float:left;'>
                                    <span>Pickering:</span> <input type='text' style='width:30px;' name='pickering_availability'  /> <br/>
                                    <span>Barrie:</span> <input type='text' style='width:30px;' name='barrie_availability'  /> <br/>
                                    <span>Kitchener:</span> <input type='text' style='width:30px;' name='kitchener_availability'  /> <br/>
                                </div>
                                <div style='clear:both;'></div>
                            </td>
                          </tr>");
                    echo "</table>";
                    echo "<input type='submit' name='submit' value='Add' />";
                    echo "</form>";

可能是,出于某种原因,您使用的 javascript 验证脚本实际上删除了-- enctype="multipart/form-data" --行,因此即使您在视图中设置了它,也只需检查生成的 html 并确保...

I can't see a problem with that. Are you sure it is just that that you changed? I agree with Damien Pirsy's comment about not echoing it. Possibly try removing the . before the filename in the action as this is not "normal" and may be confusing the browser.

echo "<form method='post' enctype='multipart/form-data' action='/add_product.php'>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM