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

File upload: Size validator or setMaxFileSize()


For Zend_Form_Element_File, is there any difference between adding a Size validator and using setMaxFileSize?

$file->addValidator('Size', false, 1000000);

$file->setMaxFileSize(1000000);

Solution

  • setMaxFileSize(1000000) will limit the size on the client side, i.e. the special MAX_FILE_SIZE tag will be created in the html, e.g.:

     <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    

    However this is not so secure because it is easy to change your html. Anyway, if you don't specify setMaxFileSize, ZF will automatically create it with value that is equal to the value of upload_max_filesize in you php.ini.

    AddValidator performs more secure validation as you cannot change the value of maximum file size in your html. Also you can specify custom error messages when you use validator.