Apache/PHP: $_FILES Array mysteriously empty

Frustrating moment of the day:

A file upload form that’s been working for a long time, suddenly fails. No file uploads work on the entire apache installation. Checking the drive space in the /tmp folder, even though 85MB are left, freeing up space reveals that if there isn’t a lot of free space left for /tmp, the $_FILES array just goes empty without further explanation. No useful error messages, or anything.

Freeing up space for /tmp solves the problem effectively.

Update: Since so many commenters have posted other problems that cause similar effects, here is a quick summary of potential issues that you should check:

  1. Check php.ini for file_uploads = On, post_max_size, and upload_max_file_size. Make sure you’re editing the correct php.ini – use phpinfo() to verify your settings. Make sure you don’t mispell the directives as 8MB instead of the expected 8M!
  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 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. Do not use javascript to disable your form file input field on form submission!

  4. Make sure your directory has read+write permissions set for the tmp and upload directories.
  5. Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.
  6. Make sure your file destination and tmp/upload directories do not have spaces in them.
  7. Make sure all FORMs on your page have /FORM close tags.
  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.

104 thoughts on “Apache/PHP: $_FILES Array mysteriously empty

  1. I think this is a php error. I do not remember the version, but it is given in the comments of the file error codes part. It is nothing to do with the empty place of your drive.

  2. According to what i’ve read on the manual, it seems to be caused by having

    file_uploads = off

    in the php.ini, or (as it was in my case), having no free space available. I would presume there are multiple possibilities, but this exact problem was what I ran into, and it was remedied by freeing space.

  3. Don’t forget to always have the form tag include the enctype=”multipart/form-data” If you don’t put this in, you won’t see anything in your _FILES var. This isn’t your issue, I know, but I wanted it posted for posterity :) (in case I forget again :) )

  4. I use method=”post”, have ample space, fileuploads is on, but the $FILES superglobal remains empty, no matter what I do. I can’t figure it out

  5. I had the same problem. The solution was that the

    is needed for the very first place in the FORM statement — my failure was that the ENCTYPE tag was put later into an statement.

  6. Oops, my code had been deleted by the robot. So, in short, the FORM statement has to contain the ENCTYPE tag, not the INPUT statement.

    Hope this helps.

  7. Uhhhgh! I’m an idiot! Aside from needing to have enctype=multipart/form-data, I found that i had made my MAXFILESIZE hidden tag set too low, PHP parses this and will spit out 2 as the value for the error element in the files hash.

    Hope this relieves future headaches =P

  8. I have the same problem. I am running PHP on IIS (Both V. 5). The form will lose the file field data if enctype is included. And nothing happens either way with or without the enctype. :-( Help Me. Pleeezzzz

  9. i have the same problem, but my upload file worked fine, until i changed dir of the file.

  10. I have the same problem as CRAIG. I have IIS 6.0 and PHP 5.02, the form is with enctype=multipart/form-data, file_upload is on and I don’t kniw what it happens!!! help meeeeeeeee!!!!! :)

  11. make sure that your enctype=”multipart/form-data” has actual double-quote characters surrounding “multipart/form-data”. I tried pasting the value out of this thread and I saw my syntax highlighting was wrong–I had pasted in smart-quotes. Smart-quotes (&#8221) won’t function. Type it by hand.

  12. I just want to thank you. Having spent hours and hours growing frustrated with my empty $_FILES array, having reread my code a thousand times, I saw the comment about enctype=”multipart/form-data” — which was entered correctly… except for the opening quotation mark. I must have read it a thousand times and not noticed that.

    Very stupid mistake that sent me scouring for obscure solutions. As dumb as it was, it was this page that made me notice it and helped me solve it.

    Thanks. :)

  13. I all, I also though I was going mad with an empty $_FILES array after a file has been uploaded. I checked all the other parameters as mentioned here but they didn’t solve the problem. It turned out that my ‘clever’ idea to disable the file input on form submission was doing the damage. The idea was to stop a user re-submitting the image upload. Just thought I would mention it because it still appears as though the image is being uploaded, but it’s not!

  14. I still have this problem. Here is my form. File uploads is on, and the php.ini file is fine. anyone know what this problem is? Could it be this in the php.ini file?

    Should i manually give it a directrory?

    ;uploadtmpdir =

    “>

    Upload PIC no bigger than KB

    “>

  15. I had the same problem. I finally discovered that the file permission on the folder I was trying to write to had been changed to read only. Further experimenting showed that if you want to upload the file to a directory outside your current folder, you need to make sure the folder above it isn’t read only as well.

    Hope that helps.

  16. thanks Mark McCray!! your post for posterity has helped me and i guess it will be usefull for more people :) . Cool that there are people like all of you in the net!!

  17. Also make sure that the file you are uploading does not have any non-alpha numeric characters in it.

    I tried uploading a file with spaces in it and got the same empty $_FILES variable.

  18. And don’t forget to use the method=”POST” in your form !

    Posting it here as this was the mistake I made!! :o )

  19. I am running into the same empty $_FILES to the PHP website I am uploading too. I am creating file upload using VB.NET in desktop application.

    Any idea how I can fix this Issue?

  20. One problem with larger PHP file uploads I have run into is uploadmaxfilesize variable in php.ini. In my configuration it was set to 2MB as a default. Check it out.

  21. Stephen Weir: thank you! my test directory had spaces in it. Now I must find a way to let the users have spaces in its directories…

  22. I was using some javascript to modify the encType to multipart/form-data and foolishly thought it would be form.encType = … would do. Turns out its form.encoding = ..

    doh.

  23. Nobody has mentioned the one config issue that got me… Checkout php.ini’s postmaxsize !!! Just FYI…

  24. Thank you so much for this thread! I had the enctype issue. This is not heavily documented anywhere ‘on the net’ (or maybe I am just that ignorant!) but makes good sense.

  25. Same problem here.

    postmaxsize directive did it for me. Annoying that this error cannot be handled in the same way as uploadmaxfilesize can be using $_FILES.

  26. Cody, thanks for mentioning the postmaxsize setting. I had never heard of it before and it was the one that made it impossible for me to upload big files to my server despite having set upload max filezise.

    There are a lot of valuable information discussed in these commets, it’d be nice to sum up all that up

  27. thank you!!!!!!! it’s 2 o’clock in the morning and it’s about 6 hours that I was looking my monitor trying to solve the problem…..

    ENCTYPE tag missing…….

  28. Hi sorry but my english is not very well I have a problem with my upload it not function I have a directory in php.ini for tmp file and this function file_uploads is on.My form have got a good enctype and the directory where I uploaded file have a right chmod IF you want to see my php code you can visit this site: http://phpfi.com/294027

    I hope that you have got understand anything

    Thanks for all reply

  29. I’ve read this list of possible reasons 10 times (no joke).

    I’ve got all these basis covered. Set all the right php.ini flags, got the ecntype tag, my tmp and upload directories are rwx.

    It’s been 4 days I’ve been going over this. I simply am stumped.

    Are there ANY other reasons the $_FILES array could end up empty?

    Started to forum thread’s here….but not a single response in days.

    http://www.php-forum.com/phpforum/viewtopic.php?f=2&t=9013

    and

    http://www.weberforums.com/ftopic11839.html

    If anyone happens to have any ideas, please drop a line at one of those forums, or here. I’d forever be in your debt.

  30. Just figured out one of my issues with $FILES. I was using the wrong $variablename is the form $FILES[$variablename][tmp_name]. Look out for typos! My $variablename was pic3 and I was looking in pic1. Arrrghhh.

  31. Also, to REALLY check if your $FILES is empty, use printf($FILES); That’s how I found I was using the wrong $variablename in $FILES[$vaiablename]['tmp_name'].

  32. I have this same problem. Makes me stuck for 2 hour. Tried all combination. All settings are correct. At last its a silly mistake. The input field have only the id attribute and name attribute is missing.

  33. yup, cool. my problem was that i had two forms in one file and need to make the other one enctype. thanks!!

  34. Thank you Venimus!! I had just added another form to the page and the upload in the former form has stopped working. I would have never thought I had to set the enctype to the second form too! Thank you very much!!

  35. 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. HTH

  36. Thanks you Andy! I had the same $_FILES problem but only with IE (IE7, didn’t try with other versions but i guess it’s the same macroshit feature). The solution was to add : form.setAttribute(“encoding”,”multipart/form-data”); near form.setAttribute(“enctype”,”multipart/form-data”); which is more w3c compliant I think.

  37. Hi,

    I am a huge fan of PHP and have been for years, however i’m rather dissapointed and frustrated by the poor capabilites regarding file uploads.

    Ive already given up on trying to get a progress bar without using extensions.

    ANYWAY

    A thing that occured to me and exlains the inconsistent behavior is that most people are ignoring the postmaxsize directive.

    my $FILES array was empty at 16M file upload even tho my uploadmax_filesize = 32M

    I discovered my postmaxsize = 16M

    and this empties the POST array. If postmaxsize is greater than uploadmaxfilesize you get the error data in the array for that file upload as expected.

    Hope this helps anyone.

    Jon

  38. Sorry not the POST array, the FILES array.

    The post max size directive will cap the total for the entire POST operation, take this into account when uploading multiple files

    i.e

    upload max = 8 post max = 16

    file upload 1 = 6 file upload 2 = 6 file_upload 3 = 7

    all less than 8 meg each so thats fine, but entire post > 16

  39. This one baffled me too. I had everything configured right. BUT, I was configuring the wrong php.ini . I was mucking about with /etc/php5/cli/php5.ini . Turns out the live config file was /etc/php5/apache2/php.ini . You can see your config file path by doing phpinfo(); in a script and taking a look at the output.

  40. I have something different than everyone else.

    I have a form that uploads a file. no problem. But when I return to the page to edit the picture I just assigned to a certain record in the DB it will stay blank when I submit the form.

    Anyone know what I can do? Tried all the options above.

  41. Even I had the same problem once, and spent a lot of time trying to figure out what was going wrong. Later I found that the issue was with not having set the form attribute enctype=”multipart/form-data”. Then I just blogged about it so that someone else in the same situation could easily find a fix for it.

  42. my $_Files variable was empty and i have checked alot of setting but it doesn’t work out

    Finally i change the double quotes to single quotes. that is change enctype =”multipart/form-data” to enctype=’multipart/form-data’ – and it works out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">