Limiting the uploded file size

How do I limit the uploaded file size. I have tried the following validator but it has not helped. It uploads files of all sizes:

...
 //Validator 
        $inputFilter->add([
            'name'     => 'my-file',
            'required' => true,               
            'validators' => [
                [
                    'name'    => MimeType::class,
                    'options' => [
                        'mimeType' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','application/octet-stream'],
                    ],
                ],
                [
                    'name'    => Size::class,
                    'options' => [
                        [
                            'min' => '10kB',
                            'max' => '40kB',
                        ]
                    ],
                ],
            ],
        ]);


What is the appropriate way to use the Size validator?
Thanks!

The configuration looks correct. Please check your input filter with a debugger or create a separate script to check if the Size validator works stand-alone as described in the documentation:

Which debugger would you recommend, please. Thx

Your IDE and Xdebug:

https://xdebug.org

Hi, I don’t like using arrays in config … this is working filter example.

use Laminas\InputFilter\InputFilter;
use Laminas\InputFilter\FileInput;
use Laminas\InputFilter\Input;

class DownloadFilter extends InputFilter
{

    public function __construct()
    {
        $inputFile = new FileInput('fileupload');
        $inputFile->setRequired(true);
        $inputFile->getValidatorChain()
            ->attachByName('filesize', array(
            'max' => '12MB'
        ));
        $inputFile->getFilterChain()->attachByName('filerenameupload', array(
            'target' => './path/tmp/',
            'overwrite' => true,
            'use_upload_name' => true
        ));
        $this->add($inputFile);

        $note = new Input('name');
        $note->setRequired(true);
        $this->add($note);
    }
}
1 Like