SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quickform incomplete file upload

    I use Pear::Quickform to do a file upload. I noticed that when the user presses stop during the file upload Quickform does not recognize that the file was only partially uploaded and validates the form.

    I'm using the following code:
    Code:
    require_once 'HTML/QuickForm.php';
    
    $action = $_SERVER['PHP_SELF'].'?upload=1&agree=1';
    $form = new HTML_QuickForm('UploadForm', 'post', $action, '_self', 'class="small"' );
    
    //define elements in upload form
    $file =& $form->addElement('file', 'filename', 'Filename:', array('size' => 50, 'maxlength' => 255, 'class' => 'klein'));
    
    $form->addElement('submit', null, 'Send', 'class="small"');
    
    // Define filters and validation rules
    $form->setMaxFileSize('16777216'); 
    $form->applyFilter('__ALL__', 'trim');
    
    $form->addRule('filename', 'No file was uploaded, or file was larger than 16MB', 'uploadedfile');
    $form->addRule('filename', 'File was larger than 16MB', 'maxfilesize', '16777216');
    $form->addRule('filename', 'The uploaded track is not recognized as an MP3 file', 'mimetype', array('audio/mpeg', 'audio/mp3', 'audio/x-mpeg', 'audio/mpg', 'application/force-download'));
    
    // Try to validate a form 
    if ( $form->validate() && $file->isUploadedFile() ) {
    	//database insertion and file moving code here
    }
    I've checked the code in /Quickform/file.php and found the following code in it:

    Code:
        /**
          * Checks if the given element contains an uploaded file
          *
          * @param     array     Uploaded file info (from $_FILES)
          * @access    private
          * @return    bool      true if file has been uploaded, false otherwise
          */
         function _ruleIsUploadedFile($elementValue)     {
             if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||             
    (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) {
                 return is_uploaded_file($elementValue['tmp_name']);
             } else {
                 return false;
             }
         } // end func _ruleIsUploadedFile
    I assume that even with a partial file upload the 'tmp_name' is set and thus it evaluates as true, even though 'error' is set to a different value than 0...

    Is this true? Should I rewrite the Quickform code and inform the authors? Or am I overlooking something?

    I suppose I can fix this easily by checking the $_POST variables directly, but if this really is a bug in quickform I'd rather inform them about it and do a little contribution that way

  2. #2
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm it seems to be caused by a bug in PHP itself... the error code UPLOAD_ERR_PARTIAL in the $_FILES array is not set correctly.

    I noticed that when I do a simple POST upload to a script containing the code
    <?php
    $message = print_r( $_FILES, TRUE );
    mail('email@address.com','test',$message);
    ?>

    and press stop during the upload it is not recognized as a partial upload, but returns [error] => 0!!

    This was already discovered in 2002 (see bug 19556 ) but is still not fixed yet.

    It is also a bug in PHP5.0 (see bug 28015)

    I'm using PHP 4.3.8 on Apache 1.3.31.

    Anyone else experiencing this problem??

  3. #3
    SitePoint Enthusiast thechronic's Avatar
    Join Date
    Jun 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I solved it!

    It appears to be an incompatibility with Gzip 1.3.26.1a. After I recompiled Apache without Gzip support the error does not occur anymore.

    When the user now presses stop the script is not executed anymore, which is probably the desired behaviour. Gzip seems to ignore the fact that the user pressed stop and passes the incomplete form to PHP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •