Processing files in HTML_QuickForm

Posted on 2008-11-11
Last Modified: 2013-11-10

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().

Question by:deafpanda
    LVL 8

    Expert Comment

    Check the link:

    You will found almost everything related to html quickform in this website.

    Author Comment

    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.

    LVL 8

    Expert Comment

    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
    // Display or process the form
    if ($form->validate()) {
    } else {
    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


    Author Comment

    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
    LVL 8

    Accepted Solution

    I think your code should work.

    Just see the line 36 of my code snippet

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now