File input not working PHP script

Hi,

I have a form page which calls a PHP script, which in turn does three things- adds the submitted details to a MySQL db, emails a notification to the admin, and uploads a file to the server.

One of the fields is a file input with browse button:


<input type="file" name="_Photo" class="formfield" />

For some reason, if I browse to and pick a file when submitting the form, it uploads to its directory on the server without any problems, but it doesn’t appear in the db as [filename].

When I add an echo $query in the script that updates the db, I see that the value for this input is empty. Clearly it isn’t even getting the value through from the form. Any ideas why not? The pathname to the file displays correctly in the input, and the file uploads without any problem.

http://us3.php.net/manual/en/features.file-upload.post-method.php

^read, it shows you the construction of the array that is created inside your files array.

The problem is, when I used _FILES instead of _POST when pulling in the data from the form into the script- I then checked the db table and saw that it had entered the values for the field as “Array”.

What I want it to do is just pick up the filename of the file I browse to when filling in the form. Any idea why it’s not doing that?

Thanks, that seems to work well- I just needed to change _Photo1 to _Photo.

I also tried nesting ifs with $target2 and $target3 but even though it worked, it looked a bit messy…

Thanks again

Information about file uploaded via

<input type="file" name="_Photo" class="formfield" />

is stored in $_FILES array and not in $_POST array, are you using $_FILES array for accessing values sent by form to your script?

Quite good tutorial about uploading files with PHP is here.


foreach(array('_Photo1','_Photo2','_Photo3') AS $key) {
  $target = "../images/items/". basename( $_FILES[$key]['name']) ;
  if(move_uploaded_file($_FILES[$key]['tmp_name'], $target));
}

I’ve managed to get a little further by creating new variables in the script that accepts the form:


  $photo1 = basename( $_FILES['_Photo']['name']);
  $photo2 = basename( $_FILES['_Photo2']['name']);
  $photo3 = basename( $_FILES['_Photo3']['name']);

Then I enter each of these into the db using the query- so now it’s accepting the filenames of all three photos.

Problem now is I’m trying to get it to upload more than just the first photo. I’ve created the following in the script:


		$target = "../images/items/"; 
$target = $target . basename( $_FILES['_Photo']['name']) ; 
		$target2 = "../images/items/"; 
$target2 = $target2 . basename( $_FILES['_Photo2']['name']) ; 
		$target3 = "../images/items/";
$target3 = $target3 . basename( $_FILES['_Photo3']['name']) ; 
		 

echo $target2; //these both display the correct filename and location
echo $target3;


But I reckon I need to get a line like this:


if(move_uploaded_file($_FILES['_Photo']['tmp_name'], $target)) 

  • for the b2nd and 3rd photos so that they upload ok too. Only problem is, move_uploaded_file doesn’t seem to accept more than one photo- so not sure what the nest way would be of getting the 2nd and 3rd photos uploaded.

Getting there tho…