Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Processing files in HTML_QuickForm

Posted on 2008-11-11
5
Medium Priority
?
529 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:deafpanda
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:biztiger
ID: 22929523
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
 

Author Comment

by:deafpanda
ID: 22939672
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
 
LVL 8

Expert Comment

by:biztiger
ID: 22939936
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
 

Author Comment

by:deafpanda
ID: 22940207
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
 
LVL 8

Accepted Solution

by:
biztiger earned 2000 total points
ID: 22942085
I think your code should work.

Just see the line 36 of my code snippet
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question