views:

56

answers:

3

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>
<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";
?>
+6  A: 

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.

As far as your php.ini setup, sometimes on systems multiple php.ini files exist. Be sure you are editing the correct one. I know you said you have configured your php.ini file to have file uploads, but did you also set your upload_max_file_size and post_max_size to be larger than the file you are trying to upload? So you should have:

file_uploads = On; sounds like you already did this
post_max_size = 8M; change this higher if needed
upload_max_file_size = 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.ini changes?

Bigwebmaster
+1, for the number of time I forgot to put "enctype='multipart/form-data'.
Dominique
+1: For restarting Apache server tip. Many Windows users forget that.
shamittomar
+7  A: 

Although very rare, but from the 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.

And here's the check-list for file uploading in PHP:

  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 misspell the directives as 8MB instead of the expected 8M!
  2. Do not use javascript to disable your form file input field on form submission!
  3. Make sure your directory has read+write permissions set for the tmp and upload directories.
  4. Make sure your file destination and tmp/upload directories do not have spaces in them.
  5. Make sure all FORMs on your page have /FORM close tags.
  6. 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.
  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. Also make sure that the file you are uploading does not have any non-alpha numeric characters in it.
  10. 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.

Finally, try uploading very small files.

shamittomar
wow :) Well, if your solution isn't here...
Paulocoghi
This doesn't apply to the discussion starter, but I had hosting provider who had their firewall policy too strict which caused some uploads to fail.
The Pixel Developer
A: 

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:

post_max_size = 2G

or

post_max_size = 2048M

If I change it to 2047M, the upload works.

elmonty