Processing files in HTML_QuickForm

Hi,

Can someone explain how to check if a file has been uploaded and then store that file in a folder using HTML QuickForm?

I'm not sure what teh syntax is to use with isUploadedFile() and moveUploadedFile().

Thanks
deafpandaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

biztigerCommented:
Check the link:
http://www.devarticles.com/c/a/Web-Graphic-Design/Using-HTML-Quickform-for-Form-Processing/10/

You will found almost everything related to html quickform in this website.
0
deafpandaAuthor Commented:
Thankyou.  I've seen that page, and unfortunately it doesn't have any info about processing files.  What I want to do is to check if a file has been uplaoded, check the file type, rename it and then move it to a folder (which may need to be created if it does not exist).  I haven't been able to find any information on how to do this.

Thanks
0
biztigerCommented:
Surprised! check the code snippet
$form = new HTML_QuickForm('uploader');
 
// File upload needs an element of type "file"
$form->addElement('file','uploaded_file','Your File:');
 
// We want files of 128kbytes or less
$max_size = 131072;
 
// Make sure that a file is uploaded
$form->addRule('uploaded_file','Please upload a file','uploadedfile');
 
// Have HTML_QuickForm test, after the file is uploaded, that it is
// less than 128k
$form->addRule('uploaded_file','Your file is too big','maxfilesize',$max_size);
$form->addElement('submit','save','Send It');
 
// Tell well-behaved browsers not to allow upload of a file larger than
// 128k
$form->setMaxFileSize(131072);
// Display or process the form
if ($form->validate()) {
   $form->process('move_file');
} else {
   $form->display();
}
 
function move_file($data) {
  global $form;
 
// Remove backslashes and forward slashes from new filename
$new_name = strtr($data['uploaded_file']['name'],'/\\','');
// Remove ".." from new filename
$new_name = str_replace('..','',$new_name);
 
$file =& $form->getElement('uploaded_file');
if ($file->moveUploadedFile('/tmp',$new_name)) {
  print "The file has been uploaded to /tmp/$new_name.";
} else {
  print "The file could not be uploaded to /tmp/$new_name.";
}
} 

Open in new window

0
deafpandaAuthor Commented:
Thanks, this looks very useful.  I will test it out when I have time later this afternoon.  Just one other thing - uploading a file is optional for the form I am creating.  To test for an uploaded file - is this syntax correct?

$file =& $form->getElement('uploaded_file'){
if ($file->isUploadedFile){
    //do stuff
}
0
biztigerCommented:
I think your code should work.

Just see the line 36 of my code snippet
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.