7

重複の可能性:
phpで33554432バイトの許容メモリサイズが使い果たされました(43148176バイトを割り当てようとしました)

コードを実行すると、次のエラーが発生しました。

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

も試してみましたini_set('memory_limit', '128M');が、うまくいきませんでした。私の php.ini ファイルのメモリ制限は 128MB です。助けてください。

コードを追加しています。私は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('as@aaa.com' => 'jf faz'))
  ->setTo(array('acc@aa.com'))
  ->setBody($result['text'], 'Here is the message itself');

$result1 = $mailer->send($message);


?>
4

2 に答える 2

21

試してみるini_set('memory_limit', '256M');

134217728 バイト = 128 MB

または、コードを書き直してメモリの消費量を減らします。

于 2012-10-05T16:46:17.097 に答える
7

128M == 134217728、あなたが見ている数。

メモリ制限は正常に機能しています。32 バイトを割り当てようとした場合、失敗する前に最後の操作で要求された量です。

巨大な配列を作成したり、大きなテキスト ファイルを読んだりしていますか? その場合は、不要になったメモリを解放するか、タスクを小さなステップに分割することを忘れないでください。

于 2012-10-05T16:47:26.393 に答える