Skip to content

Instantly share code, notes, and snippets.

@tassoevan
Last active August 29, 2015 14:06
Show Gist options
  • Save tassoevan/da8fb3654a38fb88dc8b to your computer and use it in GitHub Desktop.
Save tassoevan/da8fb3654a38fb88dc8b to your computer and use it in GitHub Desktop.
How to detect uploads exceeding POST maximum size in Slim framework
<?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