I'm trying to configure a server to accept very large uploads (1g). I think I've got the relationship between post size and max file size and the need to configure adequate script running time under control; however, I'm not clear on the the relationship between memory_limit to max post/file size.
The handbook page references the php documentation saying:
Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes.
However this stackoverflow answer claims that it is not necessary to set memory_limit greater than upload_max_filesize/post_max_size referencing the php source.
The StackOverflow answer seems to match my experience of working with uploaded files in php (i.e. moving the file from tmp instead of writing $_FILES to disk). Additionally I've currently got uploads capped at 1g and memory capped at 256m with everything appearing to work; however, I'm concerned about unforeseen ramifications of these settings given the documentation.
TL;DRCould someone comment on the qualifications around the "generally" in the handbook / php docs?
Comments
The reason we need to have
When we need to have memory_limit larger than upload limit is php processing tasks such as image resizing.
If you upload 1 gig file and want to resize it, no-doubt that you'll stuck there.
But if the file is just a mp3(lol), video or anything that php doesn't process, it wouldn't be a problem.
What's new and changing in PHP 8.4
A subtle but important distinction
That was my thought as well, at first. If, for example, I had a max of 10 upload files, a post size of 1gb and a file size of 100mb with a php memory limit of 64mb I'd clearly have a problem when I tried to work with 100mb worth of picture in 64mb worth of memory. This is the scenario you've outlined. This situation isn't a problem for me as I've got no intention of working with the 1gb file in php.
That said the docs/handbook actually reference the post limit, not the upload file size limit. I read this as saying that the same 64mb memory limit, but now configured with a 10mb file size cap, 1gb post size cap and a 100 uploaded files cap, could be a problem. This is obviously quite different, because working with any 1 of 100 10mb pictures in 64mb should be fine (ignoring whatever the actual system overhead is, the key bit is that 10 < 64 < 100); instead this suggests that the whole POST should fit into the memory_limit, and that would be a problem for me.
Looking around the environment at run time I see no evidence that uploaded files are in memory. It looks to me like files are only on disk with $_FILES meta data pointing to their temporary location. That said I've got no idea what happens between the post hitting apache and my code getting run; perhaps, php really does want to load all the uploaded files into memory... What I do know is that it's working but the docs say I shouldn't be doing it for reasons other than those we both first thought of.
Any other ideas?
edit: close tag.
Subscribe
I'm researching the same thing. Thanks for all the info here.