Issues

ZF-5025: Wrong filesize validation

Issue Type: Bug Created: 2008-11-22T05:55:46.000+0000 Last Updated: 2008-12-21T05:48:13.000+0000 Status: Resolved Fix version(s): - 1.7.1 (01/Dec/08)

Reporter: Ruslan Kornev (woto) Assignee: Thomas Weidner (thomas) Tags: - Zend_Validate_File

Related issues: Attachments:

Description

Hello, first of all sorry for bad English. It's my second submit here, sorry if something isn't right.

Old style does not working

'validators' => array( array('Size', false, array(1, 1048576)) )

--

This style works 'validators' => array( array('Size', false, array('min' => '3', 'max' => '9')) )

--

But:

  1. When file is zero size error message looks like this "Minimum expected size for file '0b.txt' is '0' but '0' detected"
  2. When file size more than max allowed we got throw error 0 string(1311) "The maximum must be greater than or equal to the minimum filesize, but 0 < 3
    exception 'Zend_Validate_Exception' with message 'The maximum must be greater than or equal to the minimum filesize, but 0 < 3' in /var/www/!project/library/Zend/Validate/File/Size.php:235 Stack trace: #0 /var/www/!project/library/Zend/Validate/File/Size.php(303): Zend_Validate_File_Size->setMax('9B') #1 /var/www/!project/library/Zend/File/Transfer/Adapter/Abstract.php(589): Zend_Validate_File_Size->isValid('/tmp/phpwPiifq', Array) #2 /var/www/!project/library/Zend/Form/Element/File.php(435): Zend_File_Transfer_Adapter_Abstract->isValid('file') #3 /var/www/!project/library/Zend/Form.php(1987): Zend_Form_Element_File->isValid(Array, Array) #4 /var/www/!project/modules/default/controllers/FilesController.php(1058): Zend_Form->isValid(Array) #5 /var/www/!project/library/Zend/Controller/Action.php(503): FilesController->uploadAction() #6 /var/www/!project/library/Zend/Controller/Dispatcher/Standard.php(285): Zend_Controller_Action->dispatch('uploadAction') #7 /var/www/!project/library/Zend/Controller/Front.php(934): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #8 /var/www/!project/index.php(91): Zend_Controller_Front->dispatch() #9 {main}"

--

This validator works good with all aviable variants 'validators' => array( array('Size', false, array('min' => '3KB', 'max' => '9KB')) )

--

I think one of the problem is error in size naming convention look here

protected function _toByteString($size) { $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); for ($i=0; $size >= 1024 && $i < 9; $i++) { $size /= 1024; } return round($size, 2) . $sizes[$i]; }

--

And here

protected function _fromByteString($size) { if (is_numeric($size)) { return (integer) $size; }

$type  = trim(substr($size, -2));
$value = substr($size, 0, -2);
switch (strtoupper($type)) {
    case 'YB':
        $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
        break;
    case 'ZB':
        $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
        break;
    case 'EB':
        $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
        break;
    case 'PB':
        $value *= (1024 * 1024 * 1024 * 1024 * 1024);
        break;
    case 'TB':
        $value *= (1024 * 1024 * 1024 * 1024);
        break;
    case 'GB':
        $value *= (1024 * 1024 * 1024);
        break;
    case 'MB':
        $value *= (1024 * 1024);
        break;
    case 'KB':
        $value *= 1024;
        break;
    default:
        break;
}

return $value;

}

--

Byte ('B') is one character $value = substr($size, 0, -2);

Comments

Posted by Thomas Weidner (thomas) on 2008-11-23T13:06:18.000+0000

For a description on how to migrate your script please look into the migration chapter of Zend_File_Transfer: http://framework.zend.com/manual/en/…

For your first problem:

When I assign a size validator with "0" as option I get no error returned when I upload a file with 0 bytes... There is no exception, no error... it simply works.

The other problem of false error message has been fixed.

Posted by Thomas Weidner (thomas) on 2008-11-23T13:06:41.000+0000

Fixed with r12791

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts