Skip to content

Incorrect validation for maxSize option in the FileValidator #10648

Closed
@litvinok

Description

@litvinok

Сurrent implementation for Symfony/Component/Validator/Constraints/FileValidator.php:

    // ...
    } elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
        $size = round(filesize($path) / 1000, 2);
        $limit = (int) $constraint->maxSize;
        $suffix = 'kB';
    } elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
        $size = round(filesize($path) / 1000000, 2);
        $limit = (int) $constraint->maxSize;
        $suffix = 'MB';
    } else {
    // ...

We try upload file with size of 4 034 560 bytes (maxSize is set 4M).
It equals 3940 Kb and 3.8477 Mb.

Validation says: The file is too large (4.03 Mb). Allowed maximum size is 4 MB.

So, I think we should have following implementation for correct calculation:

  • for Kb $size = round(filesize($path) / 1024, 2);
  • for Mb $size = round(filesize($path) / 1048576, 2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueIdeal for your first contribution! (some Symfony experience may be required)Validator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions