13

I get this error when users are uploading images on my site.

error msg is "PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in /home......." How can I fix this using php.ini?

Here is my current upload php.ini settings

upload_max_filesize = 2000M ;
post_max_size = 2000M
max_file_uploads = 8

Any ideas what else I need to add to solve this error?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
daza166
  • 3,543
  • 10
  • 35
  • 41
  • 1
    It's your script that's eating too much memory with whatever it's doing, not the upload per se! – deceze Jun 11 '11 at 07:36
  • possible duplicate of [How do you debug php "Out of Memory" issues?](http://stackoverflow.com/questions/6114155/how-do-you-debug-php-out-of-memory-issues) – Gordon Jun 11 '11 at 08:09

4 Answers4

15

The optimal memory_limit value depends on what you are doing with the uploaded files. Do you read the files into memory using file_get_contents or the GD library? In that case, increase memory_limit to at least the same as upload_max_filesize, preferably more.

If you are using GD, keep in mind that GD holds the entire image uncompressed in memory. This means that it takes memory in the range of width * height * bit-depth, e.g., 1024*768*32 = 25 165 824 bits = 3 MB for a screenshot, or as much as 55 MB for a 14 megapixel image.

Some operations may need to create a copy of the image, so consider setting memory_limit to the double of what you need to keep the image in memory. Also make sure to not load all images into memory at once if you don't have to. You can free the memory used by GD by calling imagedestroy on the handle when you are done working with the image.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
15
set_time_limit(0);
ini_set('memory_limit', '20000M');

To the top of your script. Change the 20000M accordingly.

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
srikanth
  • 151
  • 2
2

Increase your memory limit from php.ini

 memory_limit = ...
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
  • Any suggestion on value if say user was to upload 6 1MB images at once? – daza166 Jun 11 '11 at 07:38
  • My default is 128Mb and I dont think it will cause problems, but you may need to try. Try uploading 6 2MB images and then increase the value until you will not get any errors. Also be sure that your users will not upload RAW images that can get over 10MB, I recommend you to decrease max upload size. – Cem Kalyoncu Jun 11 '11 at 07:43
0

Uploaded files are saved in memory, so you should also to increase memory at least the same size of the expected file.

memory_limit = 2000M // better 2200M or above, just in case.
Jared
  • 2,999
  • 1
  • 28
  • 39
SIFE
  • 5,567
  • 7
  • 32
  • 46