PHP - Empty $_Post and $_Files - When Uploading Larger Files

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.

$_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 :)

empty $_POST and $_FILE variable when uploading large files

No idea what you actually want. But you can probe the recieved content size using:

  $_SERVER["CONTENT_LENGTH"]

This should tell how big the POST request body would have been. (The number might be higher than the actual received content, in case of an aborted upload.)

Empty $_FILES and $_POST when uploading big files

Your htaccess code is correct. You can try below code in your php file then try it.

ini_set( 'memory_limit', '20M' );
ini_set('upload_max_filesize', '20M');
ini_set('post_max_size', '20M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);

Let me know if not working.

Either _POST or _FILES is empty when submitting form

It was an issue of directory vs. path.

 // The folder where the images will be stored
$uploadDir = CONTENT_PATH . '/';

Should be corrected to:

 // The folder where the images will be stored
$uploadDir = CONTENT_DIR . '/';

Where the global variables CONTENT_PATH and CONTENT_DIR are:

defined('CONTENT_PATH')
or define('CONTENT_PATH', realpath($_SERVER['HTTP_HOST']) . '/Shape_Search/public_html/img/content');

defined('CONTENT_DIR')
or define('CONTENT_DIR', dirname(dirname(__FILE__)) . '/public_html/img/content');

PHP - $_FILES empty after large file upload on Azure

PHP has a confusing was of doing file uploads. To set a larger upload size, you should set both upload_max_filesize and post_max_size. These can be independently different values, as they do different things.

post_max_size is the maximum file size that can be sent in a POST request to the PHP script. upload_max_filesize is the maximum file size allowed via any method.



Related Topics



Leave a reply



Submit