php – 检测上传的文件是否太大

前端之家收集整理的这篇文章主要介绍了php – 检测上传的文件是否太大前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的上传表格:
<form action="uploads.PHP" method="post" enctype="multipart/form-data">
    <input name="fileupload" type="file" multiple>
    <button>Upload</button>
</form>

我的最大上传大小设置如下:

; Maximum allowed size for uploaded files.
upload_max_filesize = 5M

; Must be greater than or equal to upload_max_filesize
post_max_size = 5M

如果我上传文件大于5M,则var_dump($_ FILES)为空.我能做到:

if($_FILES){
    echo "Upload done!";
}

如果文件大于5M,则不设置$_FILES.但这有点奇怪.你会怎么做?

编辑:

文件的var_dump超过5M:

array(0) {
}

var_dump文件< = 5M:

array(1) {
  ["fileupload"]=>
  array(5) {
    ["name"]=>
    string(13) "netzerk12.pdf"
    ["type"]=>
    string(15) "application/pdf"
    ["tmp_name"]=>
    string(22) "/tmp/uploads/PHPWhm8M0"
    ["error"]=>
    int(0)
    ["size"]=>
    int(352361)
  }
}
你可以查看$_SERVER [‘CONTENT_LENGTH’]:
// 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!');
}

猜你在找的PHP相关文章