开发者

PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 41007872 bytes)

开发者 https://www.devze.com 2022-12-20 15:08 出处:网络
I\'m getting this error when I try to call the mail() function. I tried adding ini_set(\'memory_limit\', \'64m\') to my index.php file - Which include()\'s all other files into that - But it didn\'t

I'm getting this error when I try to call the mail() function.

I tried adding ini_set('memory_limit', '64m') to my index.php file - Which include()'s all other files into that - But it didn't fix it.

I then tried adding a php.ini file into the root directory (where index.php resides) with the contents memory_limit = 64M which then gave me PDO Class not found errors. So I added in the PDO extensions to the php.ini file and now all errors are gone.

However, the code still fails. phpinfo() shows the memory limit has been increased to 64M but my mail() function is killing the execution of the page.

How can I fix this? :/

Mail function

private static function emailPassword(SafeString $email, $password)
{
   $subject  = 'Registration';
   $message  = 'Your password is: ' . $password . "\n";
   $headers  = 'From: registration@domain.com'     . "\r\n";
   $headers .= 'Reply-to: registration@domain.com' . "\r\n";

   $message = str_replace("\n.", "\n..", $message);  

   if (!mail($email->unsafeRaw(), $subject,开发者_开发百科 $message, $headers))
   {
      throw new Exception('Failed');
   }
}


(33 554 432 bytes) + (41 007 872 bytes) = 71.1081543 megabytes

Set your memory_limit to 96M and call it a day!


If I look at the SafeString class, I don't see a unsafeRaw() method, but there is a toUnsafeRawString() method. Did you try debugging?

btw Why would you use this class if you're using the raw values anyway? That doesn't make any sense.


If setting your memory_limit higher and higher doesn't work, maybe you have an endless-loop somewhere which allocates memory space until the limit is reached.
I'm not sure how save the str_replace() function is, but it might create an endless-loop here:

str_replace("\n.", "\n..", $message);

"\n." is being replaced with "\n.." ==> "\n.." might be replaced with "\n..." ==>.... And you are using more and more memory until you reached the limit


The characters you have provided for the replacement may be causing the str_replace function to loop endlessly.It is not a good idea either to be playing around with setting the maximum memory limit for php. Try another string function. Maybe:

$message = preg_replace("\n.", "\n..", $message);

preg_replace is very good with such characters and may not run in many useless loops without knowing what to do.

I hope this helps.

0

精彩评论

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

关注公众号