views:

100

answers:

6

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40000 bytes) in /mounted-storage/home20a/sub001/sc20063-GJYD/[...] on line 62

It looks like the PHP-script only allowes me to allocate 32 MB to the memory? This was with an example image to test what would happen if a user tries to upload a huge picture.

My question is: How can i predict that this will happen, and then alert the user that the image was too big instead of returning this error? Its a while since I tested it, but I think I tried both try-catch and @, but they both failed since this is a fatal error. The size of the example image is only 1 kB, so it won't work checking that value.


I think I solved my own problem, I can check the $width and $height of the image, and then notify the user if the image is too big, but I have another question: How do I change the allowed amout of allocated memory? I assume that my users will try to post many high-resolutioned images. I have tried ini_set("memory_limit", $bignumber), but it isn't working.

A: 

To change the memory_limit, you have to edit the memory_limit directive in your php.ini file -- it can be done in your Apache's VirtualHost, too, if you don't want the modification to be effective for your whole server, but only for one website.

But you might not always do that -- you need to be administrator of your server...

BTW, you can take a look at How to change configuration settings.


Another possibility might be to use imagemagick, and its convert executable, using one of the Program execution Functions. This way, the resizing of your image wouldn't be done by PHP.

But, here, it means you depend on an external program...

Pascal MARTIN
*memory_limit* is *PHP_INI_ALL* changable.
Gumbo
I don't have access to the php.ini-file.
Phoexo
A: 

You need to do that in the php.ini configuration file: memory_limit.

Gregory Pakosz
+1  A: 

You could edit the allowed memory size through ini_set as well, but safe mode or suhoshin patch may block it. Set up the needed memory size in php.ini at the memory_limit directive.

erenon
A: 

Edit the setting in your PHP.ini or you can also use an .htaccess (if not on Windows hosting) with the following line in it:

php_value memory_limit 32M

The .htaccess solution can hep you if you don't have access to your php.ini or if you want to change this setting in a particular area of your site. In my example the limit is raised to 32MB. Note that some host won’t support this or won't support extra large values (particularly in shared hosting environments).

AlexV
A: 

Try with

ini_set("memory_limit","50M");

Maybe your $bignumber is not specified in megabytes (hence the M).

metrobalderas
It is. [15 characters]
Phoexo
+1  A: 

If you want to increase the memory limit, this needs to be done before the interpreter is started up (i.e. you need to change it in the php.ini file - not using ini_set).

"I think I solved my own problem, I can check the $width and $height of the image, "

the gd getimagesize will return the values which will give map to the memory usage of the bitmap in GD, however you should perhaps think about limiting the upload size and max post size before the file gets to PHP

C.

symcbean
How do I change the memory limit when I don't have access to the php.ini.file?
Phoexo
As per the other posts, its possible to change some settings in .htaccess usnig php_value - but typically you can only change the values that can be modified after PHP has started.
symcbean