Why Would $_Files Be Empty When Uploading Files to PHP

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

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.

$_FILES empty after form submission

You have to add,

enctype= "multipart/form-data"

Change your form to,

<form name="upload" method="post" action="send_form_email3.php" enctype= "multipart/form-data">

$_FILES and $_POST data empty when uploading certain files

The issue you are facing is due to the fact that post_max_size is set to 8M as default in your php.ini.
As your file is 10.4MB you run into the following error:

POST Content-Length of 10237675 bytes exceeds the limit of 8388608
bytes in Unknown

Because you've reached that limit.
The trick to fixing this is to simply up that limit by changing the value. You could simply change it directly in your php.ini file to whatever you desire, i.e. 20M.

Or you could set it via your .htaccess file with:

php_value post_max_size 20M
php_value upload_max_filesize 20M

Note: I've also added the required upload_max_filesize that you will require for the bigger files :)

PHP - $_FILES array is empty

On line 101:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" />
^

This is causing the issue, the browser it keeping this form open and therefore the missing enctype is the issue. Remove or close this form properly.

Example:

<?php
if(isset($_POST['Submit'])){
$upload_results = "";
if(!isset($_FILES)){$upload_results .= "No files uploaded"; }
if($upload_results == ""){
echo "<pre>";
var_dump($_FILES);
exit;
}
}
?>
<form action='' method="post" />
<form action='' method='post' enctype='multipart/form-data'>
<table>
<tr>
<td>Choose File: </td>
<td><INPUT type='file' id='file' name='file'></td>
</tr>
<tr>
<td> </td>
<td><INPUT type='submit' name='Submit' value='Process'></td>
</tr>
</table>
</form>

This will not post any files.

$_FILES is always empty after trying to upload files

You forgot your encoding type attribute in your <form> tag:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
enctype="multipart/form-data">

PHP - empty $_POST and $_FILES - when uploading larger files

As noted in the edited question $_POST and $_FILES are empty when PHP silently discards data (happens when the actual data is bigger than post_max_size). Since HTTP header and $_GET remain intact those can be used to detect the discards.

Option a)

if(intval($_SERVER['CONTENT_LENGTH'])>0 && count($_POST)===0){
throw new Exception('PHP discarded POST data because of request exceeding post_max_size.');
}

Option b)

Add a GET parameter that tells whether POST data is present.

PHP $_POST / $_FILES empty when upload larger than POST_MAX_SIZE

Odd as it may seem, this is intentional behavior, as POST_MAX_SIZE is a low level ultimate failsafe, and to protect you and prevent DOS attacks, there's no way the server can do anything but discard all POST data when it realizes, mid-stream, that it's receiving more data than it can safely handle. You can raise this value if you know you need to receive more data than this at once (but be sure your server can handle the increased load this will put on it) but I'd suggest looking into other ways of handling your use case, hitting up against POST_MAX_SIZE suggests to me that there might be more robust solutions than one massive HTTP POST, such as splitting it up into multiple AJAX calls, for instance.

Separate from POST_MAX_SIZE there is UPLOAD_MAX_SIZE which is the php.ini setting for a single file limit, which is what I assumed you were talking about initially. It limits the size of any one uploaded file, and if a file exceeds this value, it will set $_FILES['file']['error'] to 1. Generally speaking, you want to have your site set up like this:

  • The <form> MAX_FILE_SIZE should be set to the maximum you actually want to accept for this form. While any user attempting to exploit your site can get around this, it's nice for users actually using your site, as the browser will (actually, could) prevent them from wasting the bandwidth attempting to upload it. This should always be smaller than your server-side settings.
  • UPLOAD_MAX_FILESIZE is the maximum size the server will accept, discarding anything larger and reporting the error to the $_FILES array. This should be larger than the largest file you want to actually accept throughout your site.
  • POST_MAX_SIZE is the maximum amount of data your server is willing to accept in a single POST request. This must be bigger than UPLOAD_MAX_SIZE in order for large uploads to succeed, and must be much bigger to allow more than one file upload at a time. I might suggest a value of UPLOAD_MAX_FILESIZE * 4.1 - this will allow four large files at a time, along with a little extra data. YMMV of course, and you should ensure your server can properly handle whatever values you decide to set.

To your specific question of How to tell, PHP documentation on POST_MAX_SIZE I linked to suggested setting a get variable in the form, i.e.

<form action="edit.php?processed=1">

However like I said above, if you're running into this issue, you may want to explore alternative upload methods.



Related Topics



Leave a reply



Submit