Search code examples
phpzend-frameworkzend-formzend-form-element

File upload + hash gives me a problem when size is greater than max size


I'm noticing something strange and was wondering if someone could test my code on their side and let me know if they get the same thing.

I have a real simple form (see full form code and action code below) which only has a file upload + hidden hash + submit button. The file upload has a max size limit set to 10000000 (about 9.5MB).

When I try to upload a file bigger than the limit, the form isn't supposed to validate, but I get an error in the hash token itself Value is required and can't be empty. Can someone confirm? It looks like the token is being wiped out. I'm guessing this could happen in the case of a redirect or something, but I'm not doing any redirects, unless there's something happening in the background that I'm not noticing.

Here's the form code and my action code

class Application_Form_TestForm extends Zend_Form
{

    public function init()
    {   

    $file = new Zend_Form_Element_File('file');
    $file->setDestination(APPLICATION_PATH);
    $file->addValidator('Size', false, 10000000);
    $file->setMaxFileSize(10000000);
    $this->addElement($file);

    $hash = new Zend_Form_Element_Hash('hash');
    $hash->setIgnore(true)
         ->setSalt('mysalt');
    $this->addElement($hash);  

    $submit = new Zend_Form_Element_Submit('submit');       
    $submit->setLabel('Test')
           ->setIgnore(true);            
    $this->addElement($submit);  

    $this->setAttrib('enctype', 'multipart/form-data');
    $this->setMethod('post');
}
}

In my controller, I do the usual validation

public function indexAction()
{
    $form = new Application_Form_TestForm();
    $this->view->form = $form;

    if($this->_request->isPost()){          
        echo "post";
        if($form->isValid($this->_request->getPost())){
           echo " valid";
        }
    }

}

Solution

  • This seems to be related to your post_max_size or upload_max_filesize values in your php.ini. For example, default value of post_max_size is 8M which is less than your max file size.