FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

Upload fails silently when file size exceeds limit in php.ini #426

Closed JayWalker512 closed 7 years ago

JayWalker512 commented 7 years ago

I've been toying with a barebones application using this behavior, and I discovered this problem in the following scenario:

My file upload validator was set to not allow file uploads over 1MB. Sometimes it would work and I would get a "file too large" error on the entity, but sometimes the post would succeed and the file would simply not be uploaded.

I was a bit baffled by this, and it took me entirely too long to figure out that the silent failure was due to some files exceeding the upload_max_filesize parameter in php.ini. No error is thrown, and nothing is printed in the logs referencing it. It would be very nice to have some kind of notification, particularly an error returned on the entity when this occurs.

I traced the error to here in cakephp-upload/src/Model/Behavior/UploadBehavior.php:

public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
    {
        $validator = $this->_table->validator();
        $dataArray = $data->getArrayCopy();
        foreach (array_keys($this->config()) as $field) {
            if (!$validator->isEmptyAllowed($field, false)) {
                continue;
            }
            if (Hash::get($dataArray, $field . '.error') !== UPLOAD_ERR_NO_FILE) {
                continue;
            }
            unset($data[$field]);
        }
    }

Where the hash of the error in this scenario will correspond to UPLOAD_ERR_INI_SIZE in CakePHP's Core.php file. I'm not sure if this is the right place to handle the error, but this is where I discovered it.

josegonzalez commented 7 years ago

You'll want to use one of the validation rules: https://cakephp-upload.readthedocs.io/en/latest/validation.html#uploadvalidation