PDA

View Full Version : Multiple file uploads



ItsMeOnly
11-24-2006, 02:13 PM
Hi,

I'd need to build a "frontend" for instant uploading of multiple files. The PHP backend for handling the upload is pretty easy (iterating thru $_FILES['blah'.$x] in a loop).

The frontent part is much more difficult though. Of course I can offer upload in series of perhaps five files, yet I know that my "employer" wants rather unlimited serialization (20+).

So the question is: how do I store content of a file within, say, an array consisting of names, file contents and descriptions without resubmitting the form- but rather inserting to an array via external (like pop up window) form. Can you recommend any script that would take care of that?

Twey
11-24-2006, 05:25 PM
I believe one can use an element array:
<form action="processor.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploads[]">
<input type="file" name="uploads[]">
<input type="file" name="uploads[]">
</form>... then access the elements as normal, but getting an array:
for($i = 0; $i < count($_FILES['uploads']); ++$i) {
// Do things with $_FILES['uploads'][$i]
}However, I've never done this. If it works, it will likely make your front-end code a lot simpler.

ItsMeOnly
11-24-2006, 07:01 PM
That was in deed my first though, yet my friend, whom I'll do the frontend for (image gallery), will usually upload 20 or more images, all with descriptions, author name, etc, etc... having 20 file fields isn't the answer, neither for him, nor me- I saw it done in webmail systems so I might try to reverse engineer one, though I bet it won't be easy thing to do- and secondly, problematic as copyright goes.

However I just had "gold thought": how about build a javascript object consisting of the above fields? YES, That's it! Let's say I add a button that will add entire compound, but with different input IDs. Using my nice "show/hide" code, I can then "minimize" the "compounds" while new ones are added

Twey
11-24-2006, 07:03 PM
Oh, I see what you're saying now -- the same sort of feature that can be seen with this forum's attachment feature. This is accomplished by uploading each individual file in a separate request, and appending them to a list of files.

ItsMeOnly
11-24-2006, 08:04 PM
yep, almost the way I'd like to, and pretty enough for the job, instead though I first set up to upload each and every file in one shot- basically it's about my friend filling out the descriptions of images and letting entire series upload on server while he goes out/to sleep.

Twey
11-24-2006, 08:24 PM
Unfortunately, that's not possible without simply having reams of file inputs. The value of a file input cannot be accessed or modified for security reasons, so it's not possible to store it anywhere.

ItsMeOnly
11-24-2006, 08:32 PM
however, as you suggested, one can have multiple file inputs, and they can be added on the fly too, so that's not a big obstacle.

Twey
11-24-2006, 09:42 PM
Indeed.
<form action="uploader.php" method="post" enctype="multipart/form-data">
<div id="upl">
<input type="file" name="uploads[]">
</div>
<input type="button" onclick="var e = document.getElementById('upl').appendChild(document.createElement('input')).type = 'file'; e.name = 'uploads[]';" value="Add file">
<input type="submit" value="Upload">
</form>