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
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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.