Thanks to visit codestin.com
Credit goes to github.com

Skip to content

File Constraint maxSize not working correctly in regards to php.ini #5551

Closed
@MichaelHindley

Description

@MichaelHindley

http://symfony.com/doc/current/reference/constraints/File.html

maxSize
type: mixed
If set, the size of the underlying file must be below this file size in order to be valid. The size of the file can be given in one of the following formats:
bytes: To specify the maxSize in bytes, pass a value that is entirely numeric (e.g. 4096);
kilobytes: To specify the maxSize in kilobytes, pass a number and suffix it with a lowercase "k" (e.g. 200k);
megabytes: To specify the maxSize in megabytes, pass a number and suffix it with a capital "M" (e.g. 4M).

valdiation.yml:
Cmf\FileBundle\Entity\Image:
properties:
name:
- NotBlank: ~
- MinLength: 3
file:
- File:
maxSize: 500M <<<
mimeTypes: [image/jpg, image/jpeg, image/png]
mimeTypesMessage: This is not an image.

Response when uploading 3M file:
{"children":{"name":[],"file":{"errors":["The file is too large. Allowed maximum size is 500M bytes."]}}}

Notice it says "500M bytes", according to the doc, capital M is supposed to be for megabytes.
500k doesn't work either, as a matter of fact it always goes by bytes no matter what you specify, as long as its higher than the upload_max_filesize set in php.ini.

Quick fix:
Set upload_max_filesize to something ridiculous like 500M(not recommended, but allows the validation to work).

Solution:
I'm sadly not capable of solving this, hurray for open source though =)

In short, if the validation value is higher than the one that is set in php.ini, the validation doesn't work at all for anything else than bytes, no matter if you specify "k" or "M", and the error message also reflects this behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions