Jump to content

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


zoetway

Recommended Posts

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:
[code]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);
}
}[/code]

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
Link to comment
Share on other sites

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

Then you can get the information about the file this way:
[code]
echo $_FILES['name'][0]; // File 1
echo $_FILES['name'][1]; // File 2
echo $_FILES['name'][2]; // File 3
[/code] etc.
Link to comment
Share on other sites

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.com/web-development/javascript/updated-upload-multiple-files-with-a-single-file-element/).
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 ????
Link to comment
Share on other sites

Found out wat I was doing wrong. The function [i]move_uploaded_file[/i] 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[i] copy[/i] 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!).
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.