Jump to content


Photo

Need help on file upload in PHP with HTML form: SOLVED!


  • Please log in to reply
5 replies to this topic

#1 zoetway

zoetway
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 August 2006 - 10:59 AM

Hi everyone,
In an attempt to upload multiple files from a HTML form I can't access file['tmp_name'] more than once ?!

Here's what I'm trying:
foreach($_FILES as $image){
	if($image['tmp_name'] != ""){
		echo($image['tmp_name']."<BR>");
			
		$uploadPath = '../images/products/';
		$smallImage = uploadFile($image["tmp_name"], $uploadPath);
		$mediumImage = uploadFile($image["tmp_name"], $uploadPath);
		$largeImage = uploadFile($image["tmp_name"], $uploadPath);
	}
}

I can post the rest of the functions if needed. This only gives me $smallImage on the server, $mediumImage and $largeImage don't exist (empty strings, no filename returned by uploadFile). Does this have anything to do with the time needed to upload the first image?

phpversion = 4.3.10
provider = Lycos.nl

Thanks in advance for looking at my code attempt.
--Rob

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 August 2006 - 02:03 PM

Make sure that your form fields got names like
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />

Then you can get the information about the file this way:
echo $_FILES['name'][0]; // File 1
echo $_FILES['name'][1]; // File 2
echo $_FILES['name'][2]; // File 3
etc.

#3 zoetway

zoetway
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 August 2006 - 07:19 PM

Thanks Daniel0, that makes sense.
But it doesn't explain why the first call to $image['tmp_name'} succeeds and the remainig two don't. Or am I missing the point here?
The problem is I use multiple form input fields to send multiple files. Each time a file is browsed from the local drive it's displayed in a file list on the site through a javascript and it looks like the same input field used to browse the file is empty again. It's stick-man's multifile javascript (http://the-stickman....e-file-element/).

#4 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 08 August 2006 - 07:23 PM

Basically if you dont use the form field name as an array IE <input type="file" name="Image[]"> the php script will only loop through 1 of the file boxes as It has no idea there are others to look through
http://thewebsolutionprovider.com - A Solution For Everything Web!

#5 zoetway

zoetway
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 August 2006 - 11:12 AM

Right, but still, I'm using only one file box right now and call the same variable 3 times (even echo doesn't do anything). I changed the page to a form with one input box  (removed the javascript) with a name file_1, pass it through POST with $_FILE['file_1']['tmp_name'] and put it in $files. If I echo $files 3 times I get the tmp_name once and two empty lines ????

#6 zoetway

zoetway
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 August 2006 - 01:32 PM

Found out wat I was doing wrong. The function move_uploaded_file deletes files from the tmp directory that is used to store the POST $_FILES. I was trying to send that file to my upload function ::). Got around it by uploading only one file and usnig the copy function to create two duplicates which can then be resized....

Sounds like an awful lot of work for what I wanted to do. I'm sure there's an more efficient way but this'll do for now. I am using the methods described by Daniel0 and glenelkins now (thanks again both of you!).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users