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

前端 未结 21 2829
一向
一向 2020-11-21 22:37

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 PH

相关标签:
21条回答
  • 2020-11-21 23:30

    Here's a check-list for file uploading in PHP:

    1. 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 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 100M not 100MB.
    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 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[]">
      
    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 $_FILES to 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.

    Source for some of these points:
    http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/

    0 讨论(0)
  • 2020-11-21 23:30

    I ran into the same issue and found that it was my IDE was part of the issue. I was launching the debugger directly from the IDE (PHPStorm) instead of just using the browser directly. The IDE spawned URL was like this:

    "...localhost:63342/CB_Upload/index.php?_ijt=j2hcbacqepj87bvg66ncuohvne"
    

    and just using:

    "...localhost/CB_Upload/index.php"
    

    worked just fine. My set up is PC / Windows 10 / WAMPSERVER 3.0.6 64bit

    0 讨论(0)
  • 2020-11-21 23:30

    I had similar problem and the issue was in wrong value in htaccess as shamittomar mentioned.

    Change php_value post_max_size 10MB to php_value post_max_size 10M

    0 讨论(0)
  • 2020-11-21 23:31

    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:

        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.

    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.

    0 讨论(0)
  • 2020-11-21 23:31

    I got the same problem and none of theme was my error. Check in your .htaccess file, if you got one, if "MultiViews" are enabled. I had to disable them.

    0 讨论(0)
  • 2020-11-21 23:33

    Detach your form form the page you-re using into a simple php page that has the form and php code only, and test it like that.

    Any bootstrap or java script might clean out the _FILES[]. That was my case

    0 讨论(0)
提交回复
热议问题