为什么将文件上传到 PHP 时 $_FILES 会为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3586919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would $_FILES be empty when uploading files to PHP?
提问by elmonty
I have WampServer 2 installed on my Windows 7 computer. I'm using Apache 2.2.11 and PHP 5.2.11. When I attempt to upload any file from a form, it seems to upload, but in PHP, the $_FILES
array is empty. There is no file in the c:\wamp\tmp
folder. I have configured php.ini
to allow file uploads and such. The tmp
folder has read/write privileges for the current user. I'm stumped.
我的 Windows 7 计算机上安装了 WampServer 2。我使用的是 Apache 2.2.11 和 PHP 5.2.11。当我尝试从表单上传任何文件时,它似乎正在上传,但在 PHP 中,该$_FILES
数组为空。文件c:\wamp\tmp
夹中没有文件。我已配置php.ini
为允许文件上传等。该tmp
文件夹对当前用户具有读/写权限。我难住了。
HTML:
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form enctype="multipart/form-data" action="vanilla-upload.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
PHP:
PHP:
<?php
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
?>
采纳答案by elmonty
Thank you everybody for the vary comprehensive replies. Those are all very helpful. The answer turned out to be something very odd. It turns out that PHP 5.2.11 doesn't like the following:
谢谢大家的各种综合答复。这些都是很有帮助的。结果发现答案很奇怪。事实证明,PHP 5.2.11 不喜欢以下内容:
post_max_size = 2G
or
或者
post_max_size = 2048M
If I change it to 2047M
, the upload works.
如果我将其更改为2047M
,则上传有效。
回答by shamittomar
Here's a check-list for file uploading in PHP:
这是在 PHP 中上传文件的清单:
Check php.ini for:
file_uploads = On
post_max_size = 100M
upload_max_filesize = 100M
- You might need to use
.htaccess
or.user.ini
if you are on shared hosting and don't have access tophp.ini
. - Make sure
you're editing the correct ini file –
use the
phpinfo()
function to verify your settings are actually being applied. - Also make sure you don't
misspell the sizes - it should be
100M
not100MB
.
- You might need to use
Make sure your
<form>
tag has theenctype="multipart/form-data"
attribute. No other tag will work, it has to be your FORM tag. Double check that it is spelled correctly. Double check that multipart/form-data is surrounded by STRAIGHT QUOTES, not smart quotes pasted in from Word OR from a website blog (WordPress converts straight quotes to angle quotes!). If you have multiple forms on the page, make sure they both have this attribute. Type them in manually, or try straight single quotes typed in manually.Make sure you do not have two input file fields with the same
name
attribute. If you need to support multiple, put square brackets at the end of the name:<input type="file" name="files[]"> <input type="file" name="files[]">
Make sure your tmp and upload directories have the correct read+write permissions set. The temporary upload folder is specified in PHP settings as
upload_tmp_dir
.Make sure your file destination and tmp/upload directories do not have spaces in them.
Make sure all
<form>
's on your page have</form>
close tags.Make sure your FORM tag has
method="POST"
. GET requests do not support multipart/form-data uploads.Make sure your file input tag has a NAME attribute. An ID attribute is NOT sufficient! ID attributes are for use in the DOM, not for POST payloads.
Make sure you are not using Javascript to disable your
<input type="file">
field on submissionMake sure you're not nesting forms like
<form><form></form></form>
Check your HTML structure for invalid/overlapping tags like
<div><form></div></form>
Also make sure that the file you are uploading does not have any non-alphanumeric characters in it.
Once, I just spent hours trying to figure out why this was happening to me all of a sudden. It turned out that I had modified some of the PHP settings in
.htaccess
, and one of them (not sure which yet) was causing the upload to fail and$_FILES
to be empty.You could potentially try avoiding underscores (
_
) in thename=""
attribute of the<input>
tagTry uploading very small files to narrow down whether it's a file-size issue.
Check your available disk space. Although very rare, it is mentioned in this PHP Manual page comment:
If the $_FILES array suddenly goes mysteriously empty, even though your form seems correct, you should check the disk space available for your temporary folder partition. In my installation, all file uploads failed without warning. After much gnashing of teeth, I tried freeing up additional space, after which file uploads suddenly worked again.
Be sure that you're not submitting the form through an AJAX POST request instead of a normal POST request that causes a page to reload. I went through each and every point in the list above, and finally found out that the reason due to which my $_FILES variable was empty was that I was submitting the form using an AJAX POST request. I know that there are methods to upload files using ajax too, but this could be a valid reason why your $_FILES array is empty.
检查 php.ini:
file_uploads = On
post_max_size = 100M
upload_max_filesize = 100M
- 您可能需要使用,
.htaccess
或者.user.ini
如果您在共享主机上并且无权访问php.ini
. - 确保您正在编辑正确的 ini 文件 - 使用该
phpinfo()
功能来验证您的设置是否真正被应用。 - 另外,还要确保你没有拼错大小-它应该是
100M
没有100MB
。
- 您可能需要使用,
确保您的
<form>
标签具有该enctype="multipart/form-data"
属性。没有其他标签可以工作,它必须是您的 FORM 标签。仔细检查它是否拼写正确。仔细检查 multipart/form-data 是否被 STRAIGHT QUOTES 包围,而不是从 Word 或网站博客粘贴的智能引号(WordPress 将直引号转换为角引号!)。如果页面上有多个表单,请确保它们都具有此属性。手动输入它们,或尝试手动输入直单引号。确保您没有两个具有相同
name
属性的输入文件字段。如果需要支持多个,请在名称末尾加上方括号:<input type="file" name="files[]"> <input type="file" name="files[]">
确保您的 tmp 和上传目录具有正确的读+写权限集。临时上传文件夹在 PHP 设置中指定为
upload_tmp_dir
.确保您的文件目的地和 tmp/upload 目录中没有空格。
确保
<form>
页面上的所有's 都有</form>
关闭标签。确保您的 FORM 标签具有
method="POST"
. GET 请求不支持 multipart/form-data 上传。确保您的文件输入标签具有 NAME 属性。ID 属性是不够的!ID 属性用于 DOM,而不是用于 POST 负载。
确保您没有使用 Javascript
<input type="file">
在提交时禁用您的字段确保您没有像这样嵌套表单
<form><form></form></form>
检查您的 HTML 结构是否存在无效/重叠标签,例如
<div><form></div></form>
还要确保您上传的文件中没有任何非字母数字字符。
有一次,我花了几个小时试图弄清楚为什么这会突然发生在我身上。结果是我修改了 中的一些 PHP 设置
.htaccess
,其中一个(还不确定是哪个)导致上传失败并且$_FILES
为空。您可以尝试在标签
_
的name=""
属性中避免使用下划线 ( )<input>
尝试上传非常小的文件以缩小是否是文件大小问题。
检查您的可用磁盘空间。虽然非常罕见,但在这个PHP 手册页面注释中提到:
如果 $_FILES 数组突然变得莫名其妙地空了,即使您的表单看起来是正确的,您也应该检查可用于临时文件夹分区的磁盘空间。在我的安装中,所有文件上传都失败而没有警告。在咬牙切齿之后,我尝试释放额外的空间,之后文件上传突然再次起作用。
确保您不是通过 AJAX POST 请求而不是导致页面重新加载的普通 POST 请求提交表单。我浏览了上面列表中的每一点,最后发现我的 $_FILES 变量为空的原因是我使用 AJAX POST 请求提交表单。我知道也有使用 ajax 上传文件的方法,但这可能是您的 $_FILES 数组为空的有效原因。
Source for some of these points:
http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/
其中一些要点的来源:http:
//getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/
回答by Brian
As far as the HTML you appear to have set that part up correctly. You already have the enctype="multipart/form-data"
which is very important to have on the form.
就 HTML 而言,您似乎已正确设置了该部分。您已经有了enctype="multipart/form-data"
表格上非常重要的内容。
As far as your php.ini
setup, sometimes on systems multiple php.ini
files exist. Be sure you are editing the correct one. I know you said you have configured your php.ini
file to have file uploads, but did you also set your upload_max_filesize
and post_max_size
to be larger than the file you are trying to upload? So you should have:
就您的php.ini
设置而言,有时在系统上php.ini
存在多个文件。确保您编辑的是正确的。我知道您说过您已将php.ini
文件配置为具有文件上传功能,但是您是否也将您的upload_max_filesize
和post_max_size
设置为大于您尝试上传的文件?所以你应该有:
file_uploads = On; sounds like you already did this
post_max_size = 8M; change this higher if needed
upload_max_filesize = 8M; change this higher if needed
Does your directory: "c:\wamp\tmp"
have both read and write permissions? Did you remember to restart Apache after you made the php.ini
changes?
您的目录是否"c:\wamp\tmp"
具有读写权限?您是否记得在进行php.ini
更改后重新启动 Apache ?
回答by meda
It is important to add enctype="multipart/form-data"
to your form, example
添加enctype="multipart/form-data"
到您的表单中很重要,例如
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
回答by shashik493
I have a same problem looking 2 hours ,is very simple to we check our server configuration first.
我有同样的问题,看起来 2 小时,我们首先检查我们的服务器配置非常简单。
Example:
例子:
echo $upload_max_size = ini_get('upload_max_filesize');
echo $post_max_size=ini_get('post_max_size');
any type of file size is :20mb
, but our upload_max_size
is above 20mb
but array is null
. Answer is our post_max_size
should be greater than upload_max_filesize
任何类型的文件大小都是:20mb
,但我们upload_max_size
在上面20mb
但数组是null
. 答案是我们post_max_size
应该大于 upload_max_filesize
post_max_size = 750M
upload_max_filesize = 750M
回答by checkmate711
Here another cause I found: When using JQuery Mobile and the form attribute data-ajax is set to true, the FILES array will be empty. So set data-ajax to false.
这里我发现了另一个原因:当使用 JQuery Mobile 并且表单属性 data-ajax 设置为 true 时,FILES 数组将为空。所以将 data-ajax 设置为 false。
回答by Adrian Parr
Make sure your input element has a 'name' attribute.
<input type="file" name="uploadedfile" />
确保您的输入元素具有“名称”属性。
<input type="file" name="uploadedfile" />
If this is missing the $_FILES will be empty.
如果缺少此项,则 $_FILES 将为空。
回答by Luis Rosety
I was struggling with the same problem and testing everything, not getting error reporting and nothing seemed to be wrong. I had error_reporting(E_ALL) But suddenly I realized that I had not checked the apache log and voilà! There was a syntax error on the script...! (a missing "}" )
我正在努力解决同样的问题并测试所有内容,没有收到错误报告,似乎没有任何问题。我有 error_reporting(E_ALL) 但突然我意识到我没有检查 apache 日志,瞧!脚本中存在语法错误...!(缺少“}”)
So, even though this is something evident to be checked, it can be forgotten... In my case (linux) it is at:
所以,即使这是明显需要检查的东西,它也可以被遗忘......在我的情况下(linux)它位于:
/var/log/apache2/error.log
回答by AaronP
No one mentioned this but it helped me out and not many places on the net mention it.
没有人提到这一点,但它帮助了我,网络上没有多少地方提到它。
Make sure your php.ini sets the following key:
确保您的 php.ini 设置了以下键:
upload_tmp_dir="/path/to/some/tmp/folder"
You'll need to check with your webhost if they want you to use an absolute server file path. You should be able to see other directory examples in your php.ini file to determine this. As soon as I set it I got values in my _FILES object.
如果他们希望您使用绝对服务器文件路径,您需要与您的虚拟主机商确认。您应该能够在 php.ini 文件中看到其他目录示例来确定这一点。一旦我设置了它,我就在我的 _FILES 对象中得到了值。
Finally make sure that your tmp folder and wherever you are moving files to have the correct permissions so that they can be read and written to.
最后确保您的 tmp 文件夹以及您移动文件的任何位置具有正确的权限,以便可以读取和写入它们。
回答by Tahir Yasin
If you are trying to upload an array of files then you may need to increase max_file_uploads
in php.ini
which is by default set to 20
如果您要上传文件的数组,那么你可能需要增加max_file_uploads
的php.ini
是默认设置为20
Note: max_file_uploads
can NOT be changed outside php.ini. See PHP "Bug" #50684
注意:max_file_uploads
不能在 php.ini 之外更改。参见PHP“错误”#50684