Jump to content

File Upload Not Working


kingnutter

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

 

 

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.