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";
?>
Here's a check-list for file uploading in PHP:
Check php.ini for:
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 tophp.ini
.phpinfo()
function to verify your settings are actually being applied.100M
not100MB
.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: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:
Source for some of these points:
http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/
Another possible culprit is apache redirects. In my case I had apache's httpd.conf set up to redirect certain pages on our site to http versions, and other pages to https versions of the page, if they weren't already. The page on which I had a form with a file input was one of the pages configured to force ssl, but the page designated as the action of the form was configured to be http. So the page would submit the upload to the ssl version of the action page, but apache was redirecting it to the http version of the page and the post data, including the uploaded file was lost.