[php] PHP post_max_size overrides upload_max_filesize

In my site host, I have seen (via phpinfo) that

  • post_max_size = 8Mb
  • upload_max_filesize = 16Mb

This led me to think that I should be able to upload as file as big as 16Mb. However, when I do this through a post method (as normal), post_max_size takes over and declares that I sent too much.

What is the method which permits sending a file as big as 16Mb ? GET - PUT - other ?

Hope someone can clarify this for me.

Simon

This question is related to php upload filesize

The answer is


change in php.ini max_input_vars 1000


By POST file uploads are done (commonly, there are also other methods). Look into the method attribute of the form which contains the file-upload field ;)

The lowest limit of any related setting supersedes a higher setting:

See Handling file uploads: Common Pitfals which explains this in detail and how to calculate the values.


Your server configuration settings allows users to upload files upto 16MB (because you have set upload_max_filesize = 16Mb) but the post_max_size accepts post data upto 8MB only. This is why it throws an error.

Quoted from the official PHP site:

  1. To upload large files, post_max_size value must be larger than upload_max_filesize.

  2. memory_limit should be larger than post_max_size

You should always set your post_max_size value greater than the upload_max_filesize value.


post_max_size:

  • 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
  • Generally speaking, memory_limit should be larger than post_max_size.
  • PHP Default: 8M

upload_max_filesize:

  • The maximum size of an uploaded file
  • PHP Default: 2M

memory_limit > post_max_size > upload_max_filesize

PHP Default: 128M > 8M > 2M

By default, post_max_size should be 4 times greater than upload_max_filesize.
In turn memory_limit should be 16 times greater than post_max_size


upload_max_filesize is the limit of any single file. post_max_size is the limit of the entire body of the request, which could include multiple files.

Given post_max_size = 20M and upload_max_filesize = 6M you could upload up to 3 files of 6M each. If instead post_max_size = 6M and upload_max_filesize = 20M then you could only upload one 6M file before hitting post_max_size. It doesn't help to have upload_max_size > post_max_size.

It's not obvious how to recognize going over post_max_size. $_POST and $_FILES will be empty, but $_SERVER['CONTENT_LENGTH'] will be > 0. If the client just didn't upload any post variables or files, then $_SERVER['CONTENT_LENGTH'] will be 0.


The normal method to send a file upload is POST, thus also post_max_size should be 16 Mb or more.

Incidentally, also memory_limit plays a role. It should be bigger than 16Mb, but since the default value is 128Mb, you won't see this problem. Example php.ini configuration:

post_max_size = 16M
upload_max_filesize = 16M
memory_limit = 128M

Change these value in php.ini if you've access to it, otherwise you can try to change them in an .htaccess file.

php_value upload_max_filesize 16M
php_value post_max_size 16M 

This will work only if the AllowOverride settings permit it. Otherwise, you've to ask to your hosting company.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to upload

Upload file to SFTP using PowerShell This application has no explicit mapping for /error Schedule automatic daily upload with FileZilla jQuery AJAX file upload PHP How to find when a web page was last updated Summernote image upload on change event for file input element Multiple files upload in Codeigniter How to upload files on server folder using jsp How do I measure request and response times at once using cURL?

Examples related to filesize

Check file size before upload Converting file size in bytes to human-readable string How to get the size of a file in MB (Megabytes)? Find size of Git repository PHP post_max_size overrides upload_max_filesize Get file size before uploading How can I get a file's size in C++? PHP filesize MB/KB conversion Better way to convert file sizes in Python Using C++ filestreams (fstream), how can you determine the size of a file?