开发者

How to change the allowed amout of allocated memory?

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 开发者_StackOverflowcheck 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.


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...


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.


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.


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


Try with

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

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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜