php 致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 32 字节)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12750593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
提问by ashajf
Possible Duplicate:
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php
I got the following error when executing my code.
执行我的代码时出现以下错误。
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
Tried the ini_set('memory_limit', '128M');as well but didn't work. The memory limit in my php.ini file is 128MB. Pleas help.
也尝试过ini_set('memory_limit', '128M');,但没有奏效。我的 php.ini 文件中的内存限制是 128MB。请帮忙。
I am adding the code. I am new to php and please help me in solving this problem.
我正在添加代码。我是 php 新手,请帮助我解决这个问题。
<?php
require_once 'lib/swift_required.php';
$hostname = '{imap.abc.com:993/imap/ssl}INBOX';
$username = 'username';
$password = 'password';
$connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
ini_set('memory_limit', '256M');
function Message_Parse($id)
{
global $connection;
if (is_resource($connection))
{
$result = array
(
'text' => null,
'html' => null,
'attachments' => array(),
);
$structure = imap_fetchstructure($connection, $id, FT_UID);
//print_r($structure);
//array_key_exists — Checks if the given key or index exists in the array
if (is_array($structure) && array_key_exists('parts', $structure))
{
foreach ($structure->parts as $key => $part)
{
if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT')))
{
$filename = null;
if ($part->ifparameters == 1)
{
$total_parameters = count($part->parameters);
for ($i = 0; $i < $total_parameters; $i++)
{
if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME'))
{
$filename = $part->parameters[$i]->value;
break;
}
}
if (is_null($filename))
{
if ($part->ifdparameters == 1)
{
$total_dparameters = count($part->dparameters);
for ($i = 0; $i < $total_dparameters; $i++)
{
if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME'))
{
$filename = $part->dparameters[$i]->value;
break;
}
}
}
}
}
$result['attachments'][] = array
(
'filename' => $filename,
'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($connection, $id, ($key + 1), FT_UID))),
);
}
else
{
if ($part->subtype == 'PLAIN')
{
$result['text'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
}
else if ($part->subtype == 'HTML')
{
$result['html'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
}
else
{
foreach ($part->parts as $alternative_key => $alternative_part)
{
if ($alternative_part->subtype == 'PLAIN')
{
echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';
$result['text'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
}
else if ($alternative_part->subtype == 'HTML')
{
echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';
$result['html'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
}
}
}
}
}
}
else
{
$result['text'] = imap_body($connection, $id, FT_UID);
}
$result['text'] = imap_qprint($result['text']);
$result['html'] = imap_qprint(imap_8bit($result['html']));
return $result;
//echo $result;
}
return false;
}
$emails = imap_search($connection,'ALL');
rsort($emails);
foreach($emails as $email_number) {
$result = Message_Parse($email_number);
//print_r($structure);
// echo $result['text'];
//echo $result['html'];
//echo $result['attachments'];
// Create the Transport
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Bismillahhirrahmanirraheem')
->setFrom(array('[email protected]' => 'jf faz'))
->setTo(array('[email protected]'))
->setBody($result['text'], 'Here is the message itself');
$result1 = $mailer->send($message);
?>
回答by ficuscr
Well try ini_set('memory_limit', '256M');
我们会尽力 ini_set('memory_limit', '256M');
134217728 bytes = 128 MB
134217728 字节 = 128 MB
Or rewrite the code to consume less memory.
或者重写代码以消耗更少的内存。
回答by Niet the Dark Absol
128M == 134217728, the number you are seeing.
128M == 134217728,您看到的数字。
The memory limit is working fine. When it says it tried to allocate 32 bytes, that the amount requested by the last operation before failing.
内存限制工作正常。当它说它尝试分配 32 个字节时,这是失败前最后一个操作请求的数量。
Are you building any huge arrays or reading large text files? If so, remember to free any memory you don't need anymore, or break the task down into smaller steps.
您是否正在构建任何大型数组或读取大型文本文件?如果是这样,请记住释放不再需要的任何内存,或将任务分解为更小的步骤。

