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.
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 echo 'file count=', count($_FILES),"\n"; var_dump($_FILES); echo "\n"; ?>
$_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates a message if true.
$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
Here's a check-list for file uploading in PHP:
file_uploads = On
post_max_size = 100M
upload_max_filesize = 100M
.htaccess
or .user.ini
if you are on shared hosting and don't have access to php.ini
.phpinfo()
function to verify your settings are actually being applied.100M
not 100MB
.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.
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 submission
Make 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 the name=""
attribute of the <input>
tag
Try 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.
Source for some of these points:
https://web.archive.org/web/20050426084124/http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With