public function isUploadTooLarge($getPost, $getFiles, $getServerValue) { // check that post_max_size has not been reached // convert_to_bytes is the function turn `5M` to bytes because $_SERVER['CONTENT_LENGTH'] is in bytes. if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size'))) { // ... with your logic throw new Exception('File too large!'); } }
public function test__convert_to_bytes() { $this->assertSame(102400, convert_to_bytes('k', 100)); $this->assertSame(104857600, convert_to_bytes('m', 100)); $this->assertSame(107374182400.0, convert_to_bytes('g', 100)); $this->assertSame(102400, convert_to_bytes('K', 100)); $this->assertSame(104857600, convert_to_bytes('M', 100)); $this->assertSame(107374182400.0, convert_to_bytes('G', 100)); $this->assertSame(100, convert_to_bytes('', 100)); $this->assertSame(1048576, convert_to_bytes('k', 1024)); }
function qa_get_max_upload_size() { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } $mindb = 16777215; // from MEDIUMBLOB column type $minphp = trim(ini_get('upload_max_filesize')); $minphp = convert_to_bytes(substr($minphp, -1), $minphp); return min($mindb, $minphp); }
function qa_image_file_too_big($imagefile, $size = null) { if (function_exists('memory_get_usage')) { $gotbytes = trim(@ini_get('memory_limit')); $gotbytes = convert_to_bytes(substr($gotbytes, -1), $gotbytes); if ($gotbytes > 0) { // otherwise we clearly don't know our limit $gotbytes = ($gotbytes - memory_get_usage()) * 0.9; // safety margin of 10% $needbytes = filesize($imagefile); // memory to store file contents $imagesize = @getimagesize($imagefile); if (is_array($imagesize)) { // if image can't be parsed, don't worry about anything else $width = $imagesize[0]; $height = $imagesize[1]; $bits = isset($imagesize['bits']) ? $imagesize['bits'] : 8; // these elements can be missing (PHP bug) so assume this as default $channels = isset($imagesize['channels']) ? $imagesize['channels'] : 3; // for more info: http://gynvael.coldwind.pl/?id=223 $needbytes += $width * $height * $bits * $channels / 8 * 2; // memory to load original image if (isset($size) && qa_image_constrain($width, $height, $size)) { // memory for constrained image $needbytes += $width * $height * 3 * 2; } // *2 here and above based on empirical tests } if ($needbytes > $gotbytes) { return sqrt($gotbytes / ($needbytes * 1.5)); } // additional 50% safety margin since JPEG quality may change } } return false; }
function qa_post_limit_exceeded() { if (in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT')) && empty($_POST) && empty($_FILES)) { $postmaxsize = ini_get('post_max_size'); // Gets the current post_max_size configuration $unit = substr($postmaxsize, -1); if (!is_numeric($unit)) { $postmaxsize = substr($postmaxsize, 0, -1); } // Gets an integer value that can be compared against the size of the HTTP request $postmaxsize = convert_to_bytes($unit, $postmaxsize); return $_SERVER['CONTENT_LENGTH'] > $postmaxsize; } }
<?php define('ROOT_PATH', ''); require_once ROOT_PATH . 'functions.php'; restrictAccess('u'); //xuca /* input.php Input of questions into the database. */ $unparsed = ''; $lastErr = error_get_clear(); if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size'))) { echo 'File(s) too large.'; $error = true; } if (csrfVerify() && (posted("copypaste") || filed("fileupload") || posted("directentry"))) { echo '<div style="font-size:0.8em;border:solid 1px #000000;display:inline-block;padding:5px;"> <i>We are processing your questions right now...</i><br><br>'; if (posted("directentry")) { $err = ''; try { $q = new qIO(); $q->addByArray($_POST["Q"]); $q->commit(); } catch (Exception $e) { $err = "Error: " . $e->getMessage(); } if ($err == '') { echo "Questions entered successfully, with Question-IDs <b>" . arrayToRanges($q->getQIDs()) . "</b><br><br><br>"; } else {