6

I have set up upload_max_filesize and post_max_size to 32Mb in php.ini.

I am using Symfony2. I have created an entity that contains a file variable so that people can upload a file:

/**
 * @Assert\File(maxSize="3M")
 */
public $file;
  • When the file is smaller than 3Mb, the file is correctly uploaded.
  • When 3Mb the validator is working fine, displaying the normal error message "file is too big"
  • However, when file > 32Mb (post_max_size):

Fatal error: Allowed memory size of 150994944 bytes exhausted (tried to allocate 62353390 bytes) in /Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php on line 177

Is there a way to make the validator work when the file uploaded is higher than post_max_size? How is Symfony handling file uploads that are bigger than post_max_size?

j0k
  • 22,600
  • 28
  • 79
  • 90
Mick
  • 30,759
  • 16
  • 111
  • 130

1 Answers1

12

This is an issue about PHP, not Symfony. As you limit POST params size, your submit request does even not "reach" controller.

As post_max_size limit all POST params, including files, you should define post_max_size higher than upload_max_filesize.

So now, your question seems to be related to this one : How to gracefully handle files that exceed PHP's `post_max_size`?

Community
  • 1
  • 1
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • Thanks so much Pece. I think the problem is almost solved. I can now check whether the uploaded file is bigger than post_max_size using $_SERVER['CONTENT_LENGTH']. var_dump(POST) and var_dump(FILES) both return array(0){}. However, at the very end, when I want to throw an exception, I still get the Allocation Memory Error. I just don't understand why Apache is trying to allocate a file in the memory even though it is too big (>post_max_size). What should I do? Increasing the memory wouldn't make sense... – Mick Jun 12 '12 at 16:18
  • Hi Pece, I can't accept the answer yet because the problem is still not solved :-| I try to find out why the allowed memory size is exhausted even though $_POST and $_FILES are empty... If you have any ideas, that would be awesome. Do you have a trick to debug this type of error? Cheers! – Mick Jun 14 '12 at 09:45
  • There is a PHP parameter named 'memory_limit' that defines the max amount of memory a script can consume. Maybe the problem doesn't come from file upload but from what you do with file next to the upload itself... Use memory_get_usage() at many steps of your treatment to know where you use a lot of memory. – AlterPHP Jun 14 '12 at 12:51
  • Thanks! I tried this and this is quite interesting. memory_get_usage() is always very low. However the size that symfony try to allocate to the memory is the size of the file I upload...and again $_POST and $_FILES are empty. I ended up wondering about symfony itself so I tried a simple code outside symfony and within symfony... It only works outside symfony. I have accepted your answer and will post a new question with the code. Now I am pretty sure it is Symfony related. Symfony might do some stuff with the file on the background.. Thanks very much for your help. :-) – Mick Jun 14 '12 at 14:51
  • Here is the [post](http://stackoverflow.com/questions/11037115/symfony2-allocates-file-to-memory-same-code-tested-outside-and-inside-symfony) if you are interested :-) – Mick Jun 14 '12 at 16:28