PHP 手册中有一个名为 Handling file uploads 的部分.该部分有一个子部分,称为 Error Messages Explained .该小节描述了一个名为“UPLOAD_ERR_INI_SIZE”的错误:
值:1;上传的文件超出upload_max_filesize php.ini中的指令。
但是,根据我的经验,使用 UPLOAD_ERR_INI_SIZE 永远无法检查此特定错误,因为如果用户确实上传了超过 php.ini 中的 upload_max_filesize 指令的文件,则 $_FILES 超全局为空。想亲自测试一下吗?将此保存为“upload_test.php”,然后尝试上传一个低于限制的文件,然后上传一个超过限制的文件:
<?php
if (isset($_GET['submitted']) && $_GET['submitted'] === 'true')
{
echo 'Contents of $_POST:<hr><pre>';
print_r($_POST);
echo '</pre><hr>Contents of $_FILES:<hr><pre>';
print_r($_FILES);
echo '</pre><hr>';
exit;
}
$max_filesize_in_mib = min((int)(ini_get('upload_max_filesize')), (int)(ini_get('post_max_size')), (int)(ini_get('memory_limit')));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP Upload Test</title>
</head>
<body>
<h1>Upload a File (Maximum File Size: <?php echo $max_filesize_in_mib; ?> MiB)</h1>
<form action="upload_test.php?submitted=true" enctype="multipart/form-data" method="post">
<input type="file" name="upload_test">
<input type="hidden" name="random_field" value="You should see this field in the $_POST superglobal.">
<input type="submit" value="Upload">
</form>
</body>
</html>
所以我的问题是:如果您永远无法检查 UPLOAD_ERR_INI_SIZE 的意义何在?
最佳答案
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
当您的 POST_MAX_SIZE
大于 UPLOAD_MAX_FILESIZE
时,这是有意义的。
我尝试在 POST_MAX_SIZE
为 128MB
的环境中,然后我将 UPLOAD_MAX_FILESIZE
设置为 1MB
这是我得到的(如预期的那样):
Contents of $_POST: Array ( [random_field] => You should see this field in the $_POST superglobal. ) Contents of $_FILES: Array ( [upload_test] => Array ( [name] => Scan.tiff [type] => [tmp_name] => [error] => 1 [size] => 0 ) )
虽然我们没有得到文件的大小,但我们知道它超过了 upload_max_filesize
。
关于PHP:UPLOAD_ERR_INI_SIZE 的意义何在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8300331/