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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MichaelHindley opened this issue Sep 19, 2012 · 2 comments
Closed

Comments

@MichaelHindley
Copy link

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.

@fabpot
Copy link
Member

fabpot commented Jan 24, 2013

This issue looks fixed in 2.2 now, can you confirm?

@MichaelHindley
Copy link
Author

I can test it once some bundles that this bundles depends on have gone up to 2.2.

fabpot added a commit that referenced this issue Mar 13, 2013
This PR was merged into the 2.1 branch.

Commits
-------

7216cb0 [Validator] fix showing wrong max file size for upload errors

Discussion
----------

[Validator] fix showing wrong max file size for upload errors

this was because the maxSize option wasn't parsed correctly and simple string comparision could lead to wrong results, e.g. 200 > 1000M

| Q             | A
| ------------- | ---
| Bug fix?      | [yes]
| New feature?  | [no]
| BC breaks?    | [no]
| Deprecations? | [no]
| Tests pass?   | [yes|]
| Fixed tickets | [#6441,#5551]
| License       | MIT

Will apply cleanly to 2.2
@fabpot fabpot closed this as completed Mar 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants