Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

apache - 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...