Last active
August 29, 2015 14:06
-
-
Save tassoevan/da8fb3654a38fb88dc8b to your computer and use it in GitHub Desktop.
How to detect uploads exceeding POST maximum size in Slim framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$env = $app->environment(); | |
if ($env['REQUEST_METHOD'] == 'POST') { | |
$contentLength = intval($env['CONTENT_LENGTH']); | |
$postMaxSize = ini_get('post_max_size'); | |
if (preg_match('/^(\\d+)([PTGMK])$/iu', $postMaxSize, $matches)) { | |
$postMaxSize = intval($matches[1]); | |
switch (strtoupper($matches[2])) { | |
case 'P': $postMaxSize *= 1024; | |
case 'T': $postMaxSize *= 1024; | |
case 'G': $postMaxSize *= 1024; | |
case 'M': $postMaxSize *= 1024; | |
case 'K': $postMaxSize *= 1024; | |
} | |
} else { | |
$postMaxSize = intval($postMaxSize); | |
} | |
if ($contentLength > $postMaxSize) { | |
$app->error( | |
new RuntimeException( | |
"Content-length (=$contentLength) > Post maximum size (=$postMaxSize)" | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment