Search code examples
phplamp

Why does the official PHP manual incorrectly state, “Generally speaking, 'memory_limit' should be larger than 'post_max_size'?”


I am posting this question here after seeing this recent SuperUser question on the role memory_limit plays in file upload limits.

The official PHP manual states the following under post_max_size

Generally speaking, memory_limit should be larger than post_max_size.

If you understand PHP on some level and what memory_limit is versus post_max_size this official advice makes utterly no sense whatsoever. memory_limit is a setting that is concerned with PHP process memory limits and post_max_size is simply concerned with the max size of items being uploaded via PHP to a filesystem.

Now granted, one can POST a large to a PHP script and have it stored in PHP memory. But pretty much nobody does that. When the vast majority of files are POSTed to a PHP script, that file is streamed through PHP and saved to the file system.

So why then is this “Generally speaking…” advice being given? Or am I completely off my rocker after 20+ years of PHP development and somehow I have been able to upload huge files to LAMP stack applications without having memory_limit exceed post_max_size?

If found a similar question here and this answer as well as this other answer that confirms what I am saying. And heck, here is another answer to another question that touches on the topic where the answer clearly states, “No.”

So why then is the official PHP manual stating memory_limit has anything to do with post_max_size?

And yes, I understand there are some cases in which this “Generally speaking…” advice is valid, but I am finding it confusing how many online sources (see above) contradict what is said in the PHP manual. Perhaps that “general” advice should be made to be more specific for different usage cases?


Solution

  • Because if you use the $_POST array it needs to be stored inside the memory, especially for large POST data. Therefore it needs to fit into memory.

    You also could use the stream wrapper php://input to reduce memory usage instead of $_POST.