Skip to content

Commit

Permalink
bug #32790 [HttpFoundation] Fix getMaxFilesize (bennyborn)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.4 branch (closes #32790).

Discussion
----------

[HttpFoundation] Fix `getMaxFilesize`

When checking for the maximum size of an uploaded file you can't just rely on `upload_max_filesize` since the request might also exceed `post_max_size`. Also discussed in contao/contao#498

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

54107ba [HttpFoundation] Fix `getMaxFilesize`
  • Loading branch information
nicolas-grekas committed Jul 30, 2019
2 parents b706372 + 54107ba commit 1143b02
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Expand Up @@ -214,13 +214,26 @@ public function move($directory, $name = null)
*/
public static function getMaxFilesize()
{
$iniMax = strtolower(ini_get('upload_max_filesize'));
$sizePostMax = self::parseFilesize(ini_get('post_max_size'));
$sizeUploadMax = self::parseFilesize(ini_get('upload_max_filesize'));

if ('' === $iniMax) {
return PHP_INT_MAX;
return min([$sizePostMax, $sizeUploadMax]);
}

/**
* Returns the given size from an ini value in bytes.
*
* @return int The given size in bytes
*/
private static function parseFilesize($size)
{
if ('' === $size) {
return 0;
}

$max = ltrim($iniMax, '+');
$size = strtolower($size);

$max = ltrim($size, '+');
if (0 === strpos($max, '0x')) {
$max = \intval($max, 16);
} elseif (0 === strpos($max, '0')) {
Expand All @@ -229,7 +242,7 @@ public static function getMaxFilesize()
$max = (int) $max;
}

switch (substr($iniMax, -1)) {
switch (substr($size, -1)) {
case 't': $max *= 1024;
// no break
case 'g': $max *= 1024;
Expand Down

0 comments on commit 1143b02

Please sign in to comment.