Upload Doesn't Work Right When the File Is Too Big

Upload doesn't work right when the file is too big

As per the PHP docs, error code 1 is UPLOAD_ERR_INI_SIZE: "The uploaded file exceeds the upload_max_filesize directive in php.ini"

You need to make sure all the following variables are properly set:

upload_max_filesize - max size of any individual file in an upload

max_file_uploads - total number of files allowed to be uploaded

post_max_size - sum total of all data being POSTed (form data + files)

memory_limit - must be > post_max_size, to allow space for PHP + script overhead

And on top of that, there's the web server limits as well. Apache's got LimitRequestBody which would apply long before PHP ever enters the picture.

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.

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!');
}

Responsive Filemanager doesn't upload files bigger than ~2MB

I managed to find the solution for my own problem. I couldn't believe some sort of bug would cause any file exactly bigger than 2 MB to fail, so after a while I finally figured out it had to be something with the server itself, and indeed, in the php.ini I found the following line:

upload_max_filesize = 2M

Changing this to a bigger number fixed the problem for me. Would be nice if ResponsiveFileManager had a way of informing the user about the fact that the upload did in fact not complete successfully due to a php.ini server setting, but ah well...

PHP sha1_file warning if file is above 5mb

Based on the error:-

"Image must be less than 2 MB in size".

You need to increase things:-

upload_max_filesize
post_max_size

from 2M to 5M OR 10M inside php.ini file.

Increase them, save them and then restart your server and then try again.

Upload 900MB (or bigger) file: PHP, Apache, Local Network

Solution!

php.ini configuration was correct. The file was too big to be handled by a single POST. So i tried a plugin that handles a chunked-file upload.

With this apporach (and this plugin) the problem is fully solved!

https://github.com/blueimp/jQuery-File-Upload

Thanks to everyone for your support.



Related Topics



Leave a reply



Submit