PHP:UPLOAD_ERR_INI_SIZE有什么意义? [英] PHP: What's the point of UPLOAD_ERR_INI_SIZE?
问题描述
PHP手册中有一个名为处理文件上传的部分。该部分有一个小节称为错误消息说明。该小节描述了一个名为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'$ _POST的内容:< hr>< pre>';
print_r($ _ POST);
echo'< / pre>< hr> $ _FILES的内容:< hr>< pre>
print_r($ _ FILES);
echo'< / pre>< hr>';
退出; $($)
$ 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-Typecontent =text / html; charset = utf-8>
< title> PHP上传测试< / title>
< / head>
< body>
< h1>上传文件(最大文件大小:<?php echo $ max_filesize_in_mib;?> MiB)< / h1>
< form action =upload_test.php?submitted = trueenctype =multipart / form-datamethod =post>
< input type =filename =upload_test>
< input type =hiddenname =random_fieldvalue =您应该在$ _POST超级全局中看到此字段。>
< input type =submitvalue =Upload>
< / form>
< / body>
< / html>
所以我的问题是:如果你永远不能检查,UPLOAD_ERR_INI_SIZE有什么意义? p>
UPLOAD_ERR_INI_SIZE值:1;上传的文件超过了php.ini中的upload_max_filesize指令。
当您的 POST_MAX_SIZE
大于 UPLOAD_MAX_FILESIZE
。
我在一个环境中尝试过 POST_MAX_SIZE
是 128MB
,那么我将 UPLOAD_MAX_FILESIZE
设置为 1MB
这是我得到的(如预期的):
$ _POST的内容:
数组
(
[random_field] =>您应该在$ _POST超全局中看到此字段
)
内容的$ _FILES:
数组
(
[upload_test] =>数组
(
[name] => Scan.tiff
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
虽然我们没有获取文件的大小,但我们知道它超过了 upload_max_filesize
。 p>
The PHP manual has a section called Handling file uploads. That section has a subsection called Error Messages Explained. That subsection describes an error called "UPLOAD_ERR_INI_SIZE":
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
However, in my experience, it's impossible to ever check for this particular error using UPLOAD_ERR_INI_SIZE because if a user ever does upload a file that exceeds the upload_max_filesize directive in php.ini, the $_FILES superglobal is empty. Want to test it for yourself? Save this as "upload_test.php" and then try to upload a file that's under the limit and then a file that's over the limit:
<?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>
So my question is this: what's the point of UPLOAD_ERR_INI_SIZE if you can never check for it?
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
This make sense when your POST_MAX_SIZE
is bigger than UPLOAD_MAX_FILESIZE
.
I tried in an environment where POST_MAX_SIZE
is 128MB
, then i set UPLOAD_MAX_FILESIZE
to 1MB
Here's what i got (as expected):
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 ) )
Although we don't get the size of the file, we do know that it's exceeding the upload_max_filesize
.
这篇关于PHP:UPLOAD_ERR_INI_SIZE有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!