Jump to content

Basic upload code failing


graham23s

Recommended Posts

Hi Guys,

 

i just realised this piece of basic code isn't working, all i was trying to do is echo out the name of the uploaded file nothing else at this point to test its working:

 

<?php
if(isset($_POST['submit']))
{

// vars //
$product_image_size = $_FILES['product_image']['size'];
$product_image_name = $_FILES['product_image']['name'];

echo $product_image_name ;
// error checking //
//if(empty($product_image_name))
//{
// display_error("You never selected an image to upload.");
//}

} // end isset //

// Begin the administration panel //
print("<form action=\"productmanagement.php\" method=\"post\">");
print("<table width=\"60%\" border=\"1\" bordercolor=\"#f1f1f1\" cellpadding=\"5\" cellspacing=\"0\" />\n");
print("<tr>\n");
print("<th style=\"background-color: #f1f1f1;\" colspan=\"2\" align=\"left\"><span class=\"stats_header\">Upload A New Product</span></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"product_table\" align=\"right\">Product image:</td><td class=\"product_table\" align=\"left\"><input type=\"file\" name=\"product_image\" size=\"50\"></td>");
print("</tr>\n");
print("<tr>\n");
print("<td class=\"product_table\" align=\"right\">Product name:</td><td class=\"product_table\" align=\"left\"><input type=\"text\" name=\"product_name\" size=\"50\"></td>");
print("</tr>\n"); 
print("<tr>\n");
print("<td class=\"product_table\" align=\"right\" valign=\"top\">Product description:</td><td class=\"product_table\" align=\"left\"><textarea name=\"product_desc\" cols=\"50\" rows=\"20\"></textarea></td>");
print("</tr>\n"); 
print("<tr>\n");
print("<td class=\"product_table\" align=\"right\">Product category:</td><td class=\"product_table\" align=\"left\">");
print("<select name=\"product_category\">");
// query the cats //
$q1 = "SELECT * FROM `fcp_categories`";
$r = mysql_query($q1);

while($row = mysql_fetch_array($r))
{

// vars //
$cat_id = $row['id'];
$cat_name = $row['categoryname'];

// drop down box //
print("<option value=\"$cat_id\">$cat_name</option>\n");

}
print("</select>\n");
print("</td>\n");
print("<tr>\n");
print("<td class=\"product_table\" align=\"right\" valign=\"top\">Product added by:</td><td class=\"product_table\" align=\"left\"><b>$admins_name</b></td>\n");
print("</tr>\n"); 
print("<tr>\n");
print("<td colspan=\"2\" class=\"product_table\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Add Product\"></td>\n");
print("</tr>\n"); 
print("</table>\n");
print("</form>\n");
?>

 

when i press the button i the page loads to itself but doesn't echo the name of the uploaded file.

 

also i was wondering if it's better to do all my error checking for uploaded files on a seperate .php page rather than the same one.

 

thanks for any help guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/94151-basic-upload-code-failing/
Share on other sites

You haven't set the form to be multipart upload type.

 

direct from the php.net documentation

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />

 

Note the form enctype and the hidden input field.

I suggest you use both to get your PHP script working :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.