[nycphp-talk] multiple file uploading
Mitch Pirtle
mitchy at spacemonkeylabs.com
Tue Mar 2 11:22:37 EST 2004
Greg Faber wrote:
> Good morning people,
>
> Just a quick question, for which I'd like a quick answer.
>
> When I wrote (uh... copy/pasted) my script to upload a file, I included
> the usual safeguards they mention in all the books: check to see if
> something is uploaded, verify file size != 0 and verify file type. Now,
> let's say I want to be able to upload 5 files at a time, must I verify
> every possible combination of uploaded/not-uploaded file or is there a
> function out there that was made specifically to handle this? (if there
> isn't I'll just bite the bullet and do it, I'm just thinking that this
> is so common someone MUSt have done it before, right?)
I just dealt with this problem in the wee hours of last night - have a
website that lets the client upload their entire database through one
zipfile, as the six separate files were huge and it just seemed messy
that way :)
With PEAR's HTML_QuickForm, you could create the entire shebang with the
following code (apologies if I am being too verbose):
<?php
require_once 'HTML/QuickForm.php';
$form = new HTML_QuickForm('firstForm');
// create the form and it's required elements
$file =& $form->addElement('file', 'myfile1', 'Your datafile:');
$file =& $form->addElement('file', 'myfile2', 'Another datafile:');
$file =& $form->addElement('file', 'myfile3', 'And another:');
$file =& $form->addElement('file', 'myfile4', 'And another:');
$file =& $form->addElement('file', 'myfile5', 'And another:');
$form->addElement('submit', null, 'Send');
// this is the rule that makes them submit the file
$form->addRule('myfile', 'Required', 'uploadedfile', NULL, 'client');
$form->addRule('myfile', 'Required', 'uploadedfile', NULL, 'client');
$form->addRule('myfile', 'Required', 'uploadedfile', NULL, 'client');
$form->addRule('myfile', 'Required', 'uploadedfile', NULL, 'client');
$form->addRule('myfile', 'Required', 'uploadedfile', NULL, 'client');
// validate the form
if ($form->validate()) {
/*
form was validated, you got your files and can
manipulate them just like normally-submitted files
*/
print_r($_FILES['myfile1'];
print_r($_FILES['myfile2'];
print_r($_FILES['myfile3'];
print_r($_FILES['myfile4'];
print_r($_FILES['myfile5'];
} else {
// nothing submitted, give them the form
$form->display();
}
?>
The documentation for HTML_QuickForm is excellent, and inclues a
QuickStart and an external link to a more detailed tutorial:
http://pear.php.net/manual/en/package.html.html-quickform.php
I'd also like to mention that I just finished a review of "PHP
Anthology, Volume I, Foundations" by Harry Fuecks (SitePoint) that
tackles your problem with standard PHP coding style and using PEAR.
Very comprehensive and useful.
-- Mitch
More information about the talk
mailing list