kingnutter Posted June 9, 2009 Share Posted June 9, 2009 Hi everyone, I am attempting to put an image upload into my project for the first time. A lot of tutorials and examples have been very complicated. The following is adapted from the most newbie-friendly one I could find at w3schools, but I still feel a bit swamped with variables and what info needs to be posted. The following snippets of code are all from the same page containing a form and processing the data entered therein. Here is the image upload section of the form. The action of the form has been set earlier on. Do I need to have "form" preceding the enctype in this section?: <p> <td valign="top"><b><font size="-1">Add Cover Image:</font></b></td> <td> <form enctype="multipart/form-data"> <!-- <label for="file">Filename:</label> --> <input type="file" name="image_file" id="image_file" /> </td></tr></p> Here is the section which generates an errorList for the whole form. I'm not too bothered about file restrictions at the minute as this CMS is just for my own use but it would still be useful: $errorList = array(); $cover_image_upload = $_POST[image_file]; $moj_title = $_POST[moj_title]; $moj_issue = $_POST[moj_issue]; $moj_summary = $_POST[moj_summary]; $moj_genre = $_POST[moj_genre]; $day = $_POST[day]; $month = $_POST[month]; $year = $_POST[year]; if /* I will be dealing with file restrictions later ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && */ ($_FILES["file"]["size"] < 20000) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { $errorList[] = 'Invalid Image File'; } And finally the script to upload to my server. I am working locally at present. My php files are in a folder which resides in the same directory as the "images" folder where I would like the files to end up. move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $_FILES["file"]["name"]); echo "Stored in: " . "images/" . $_FILES["file"]["name"]; // generate and execute query $query = "INSERT INTO... ...this continues to insert the other field values into my database. I am a little thrown by $_FILES but expect it is a global that sorts itself out according to the file temporarily stored. Also I have tried replacing "file" with "image_file" throughout to no avail. Any pointers greatfully received. KN Quote Link to comment https://forums.phpfreaks.com/topic/161562-file-upload-not-working/ Share on other sites More sharing options...
rivan Posted June 9, 2009 Share Posted June 9, 2009 First you can't nest form elements (it sounds to me like you are nesting them) - use only one form tag with both enctype and action - after posting that form _FILES array normaly would be filled with names of temporary files created (it is superglobal variable, so PHP would take care of setting it right), after working with those temporary files you should erase them (I believe the only problem you have is with this form tag in html) Quote Link to comment https://forums.phpfreaks.com/topic/161562-file-upload-not-working/#findComment-852596 Share on other sites More sharing options...
jpratt Posted June 9, 2009 Share Posted June 9, 2009 Also the other problem is that you have to use the name of the element: <input type="file" name="image_file" id="image_file" /> so you reference it as: $_FILES["image_file"]["name"] it does matter otherwise its looking for a temp uploaded file with a name of file not image_file. Quote Link to comment https://forums.phpfreaks.com/topic/161562-file-upload-not-working/#findComment-852599 Share on other sites More sharing options...
waynew Posted June 9, 2009 Share Posted June 9, 2009 Hi. You have the name of your form field set as image_file, whereas your code is trying to find a file that has been uploaded from a file field called form. Quote Link to comment https://forums.phpfreaks.com/topic/161562-file-upload-not-working/#findComment-852602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.