Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between php’s memory_limit, upload_max_filesize and post_max_filesize

Bottom-line:
Do I need to be concerned about setting post_max_filesize >> memory_limit?

Details:
This answer suggests that uploaded files do not need to fit within php’s memory_limit. The php docs suggest that the entire post should fit within php’s memory limit.

I find the docs surprising and I’m hoping someone can elaborate. For example take the following php configs:

; config A
memory_limit = 50M
upload_max_filesize = 100M
post_max_filesize = 1000M
max_file_uploads = 10    

and

; config B
memory_limit = 50M
upload_max_filesize = 10M
post_max_filesize = 1000M
max_file_uploads = 100    

With these configurations I’d expect to be able to:

  • upload 10x100mb files to server A,
  • and 100x10mb files to server B.
I would also expect that:
  • Working with any one of the 10 files uploaded to server A is a problem (100Ms of file in a 50M bag…).
  • Working with any 1 of the 100 files uploaded to server B is okay (10 < 50).
While experimenting with less round but equivalently related numbers, I’ve found these expectations hold true.

This experience would lead me to say that "generally the memory_limit should be larger than the upload_max_filesize"; instead, the php docs say:

generally speaking, memory_limit should be larger than post_max_size.

Why and what happens if it isn't?

When my php code is executed I see no evidence that all of the posted files are in memory. It seems to me that all I’ve got is a $_FILES array of paths to files found exclusively on disk. Is php holding the whole post in memory at some point prior to my ability to introspect the environment? Do I need to be concerned about setting post_max_filesize >> memory_limit?

Aside:
Violating the manual's rule does not result in a grossly broken server (w/ php5.3 apache2.2 debian 6).

like image 403
Finn Avatar asked Feb 24 '11 15:02

Finn


People also ask

What is upload_max_filesize and Post_max_size?

upload_max_filesize is the maximum size of an uploaded file. This is the limit for a SINGLE file. post_max_size, on the other hand, is the limit of the entire body of the request (which may include multiple files as well as other stuff).

How increase upload limit in PHP INI?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads .

What is PHP post max size?

The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size.


1 Answers

PHP will accept uploaded files that are individually smaller than upload_max_filesize and together take less than post_max_size bytes. The PHP documentation is wrong with regard to memory_limit which does not need to hold the file contents posted.

The following configuration works with both Apache2 module and CGI, and accepts files smaller than 1G.

 upload_max_filesize = 1G
 post_max_size = 1G
 memory_limit = 32M
like image 76
jmz Avatar answered Oct 19 '22 19:10

jmz