为什么将文件上传到 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:19:18  来源:igfitidea点击:

Why would $_FILES be empty when uploading files to PHP?

phpapachefile-upload

提问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 $_FILESarray is empty. There is no file in the c:\wamp\tmpfolder. I have configured php.inito allow file uploads and such. The tmpfolder 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 中上传文件的清单:

  1. Check php.ini for:
    file_uploads = On
    post_max_size = 100M
    upload_max_filesize = 100M

    • You might need to use .htaccessor .user.iniif you are on shared hosting and don't have access to php.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 100Mnot100MB.
  2. Make sure your <form>tag has the enctype="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.

  3. Make sure you do not have two input file fields with the same nameattribute. 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[]">
    
  4. 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.

  5. Make sure your file destination and tmp/upload directories do not have spaces in them.

  6. Make sure all <form>'s on your page have </form>close tags.

  7. Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.

  8. 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.

  9. Make sure you are not using Javascript to disable your <input type="file">field on submission

  10. Make sure you're not nesting forms like <form><form></form></form>

  11. Check your HTML structure for invalid/overlapping tags like <div><form></div></form>

  12. Also make sure that the file you are uploading does not have any non-alphanumeric characters in it.

  13. 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 $_FILESto be empty.

  14. You could potentially try avoiding underscores (_) in the name=""attribute of the <input>tag

  15. Try uploading very small files to narrow down whether it's a file-size issue.

  16. 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.

  17. 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.

  1. 检查 php.ini:
    file_uploads = On
    post_max_size = 100M
    upload_max_filesize = 100M

    • 您可能需要使用,.htaccess或者.user.ini如果您在共享主机上并且无权访问php.ini.
    • 确保您正在编辑正确的 ini 文件 - 使用该phpinfo()功能来验证您的设置是否真正被应用。
    • 另外,还要确保你没有拼错大小-它应该是100M没有100MB
  2. 确保您的<form>标签具有该enctype="multipart/form-data"属性。没有其他标签可以工作,它必须是您的 FORM 标签。仔细检查它是否拼写正确。仔细检查 multipart/form-data 是否被 STRAIGHT QUOTES 包围,而不是从 Word 或网站博客粘贴的智能引号(WordPress 将直引号转换为角引号!)。如果页面上有多个表单,请确保它们都具有此属性。手动输入它们,或尝试手动输入直单引号。

  3. 确保您没有两个具有相同name属性的输入文件字段。如果需要支持多个,请在名称末尾加上方括号:

    <input type="file" name="files[]">
    <input type="file" name="files[]">
    
  4. 确保您的 tmp 和上传目录具有正确的读+写权限集。临时上传文件夹在 PHP 设置中指定为upload_tmp_dir.

  5. 确保您的文件目的地和 tmp/upload 目录中没有空格。

  6. 确保<form>页面上的所有's 都有</form>关闭标签。

  7. 确保您的 FORM 标签具有method="POST". GET 请求不支持 multipart/form-data 上传。

  8. 确保您的文件输入标签具有 NAME 属性。ID 属性是不够的!ID 属性用于 DOM,而不是用于 POST 负载。

  9. 确保您没有使用 Javascript<input type="file">在提交时禁用您的字段

  10. 确保您没有像这样嵌套表单 <form><form></form></form>

  11. 检查您的 HTML 结构是否存在无效/重叠标签,例如 <div><form></div></form>

  12. 还要确保您上传的文件中没有任何非字母数字字符。

  13. 有一次,我花了几个小时试图弄清楚为什么这会突然发生在我身上。结果是我修改了 中的一些 PHP 设置.htaccess,其中一个(还不确定是哪个)导致上传失败并且$_FILES为空。

  14. 您可以尝试在标签_name=""属性中避免使用下划线 ( )<input>

  15. 尝试上传非常小的文件以缩小是否是文件大小问题。

  16. 检查您的可用磁盘空间。虽然非常罕见,但在这个PHP 手册页面注释中提到:

    如果 $_FILES 数组突然变得莫名其妙地空了,即使您的表单看起来是正确的,您也应该检查可用于临时文件夹分区的磁盘空间。在我的安装中,所有文件上传都失败而没有警告。在咬牙切齿之后,我尝试释放额外的空间,之后文件上传突然再次起作用。

  17. 确保您不是通过 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.inisetup, sometimes on systems multiple php.inifiles exist. Be sure you are editing the correct one. I know you said you have configured your php.inifile to have file uploads, but did you also set your upload_max_filesizeand post_max_sizeto be larger than the file you are trying to upload? So you should have:

就您的php.ini设置而言,有时在系统上php.ini存在多个文件。确保您编辑的是正确的。我知道您说过您已将php.ini文件配置为具有文件上传功能,但是您是否也将您的upload_max_filesizepost_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.inichanges?

您的目录是否"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_sizeis above 20mbbut array is null. Answer is our post_max_sizeshould 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_uploadsin php.iniwhich is by default set to 20

如果您要上传文件的数组,那么你可能需要增加max_file_uploadsphp.ini是默认设置为20

Note: max_file_uploadscan NOT be changed outside php.ini. See PHP "Bug" #50684

注意max_file_uploads不能在 php.ini 之外更改。参见PHP“错误”#50684