$_File Upload Large File Gives Error 1 Even Though Upload_Max_Size Is Bigger Than the File Size

$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size

I think this is because of a typo. Instead of

upload_max_filesize = 7MB

it should read

upload_max_filesize = 7M

use phpinfo() again to check what value actually gets applied.

php file upload error of 1 - What is correct way to use php_ini?

Some providers does not allow you change certain values in running time. Instead of this, try to either change it in the real php.ini file or use an .htaccess (For Apache web servers) where you can add your configuration. You can find more information in the PHP.net article about this subject: How to change configuration settings.

Based on your story, example .htaccess

<IfModule mod_php5.c>
php_value upload_max_filesize 100000000
php_value post_max_size 110000000
php_value memory_limit 120000000
php_value max_input_time 20
</IfModule>

PHP File Upload greater than upload_max_filesize and error

The error is in $_FILES['userfile']['error']. You just have to check that this value is UPLOAD_ERR_INI_SIZE to detect if the file is bigger than the max size defined in your php.ini.


Resources :

  • php.net - File upload, Error Messages Explained

Detect if uploaded file is too large

You could check the $_SERVER['CONTENT_LENGTH']:

// check that post_max_size has not been reached
// convert_to_bytes is the function turn `5M` to bytes because $_SERVER['CONTENT_LENGTH'] is in bytes.
if (isset($_SERVER['CONTENT_LENGTH'])
&& (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size')))
{
// ... with your logic
throw new Exception('File too large!');
}

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.

How to gracefully handle files that exceed PHP's `post_max_size`?

From the documentation :

If the size of post data is greater
than post_max_size, the $_POST and
$_FILES superglobals are empty
. This
can be tracked in various ways, e.g.
by passing the $_GET variable to the
script processing the data, i.e. <form
action="edit.php?processed=1">, and
then checking if $_GET['processed'] is
set.

So unfortunately, it doesn't look like PHP sends an error. And since it sends am empty $_POST array, that is why your script is going back to the blank form - it doesn't think it is a POST. (Quite a poor design decision IMHO)

This commenter also has an interesting idea.

It seems that a more elegant way is
comparison between post_max_size and
$_SERVER['CONTENT_LENGTH']. Please
note that the latter includes not only
size of uploaded file plus post data
but also multipart sequences.

PHP upload 14MB file failing

You should simply increase the value of upload_max_filesize (by default it's 2M) in your php.ini file (its location depends on your operating system), and restart your web server.

Files not uploading despite not reaching the file size limit

The error you're getting is related to POST data in general (which uploads go through), and is not specific to file uploads.

The PHP setting you're looking for is post-max-size. You should be able to fix your issue by increasing its value.

Relevant part from the docs:

Sets max size of post data allowed. This setting also affects file
upload. To upload large files, this value must be larger than
upload_max_filesize.



Related Topics



Leave a reply



Submit