【问题标题】:Fatal error: Out of memory PHP致命错误:PHP 内存不足
【发布时间】:2012-08-27 00:46:55
【问题描述】:

我不知道为什么昨晚工作正常,今天早上我开始了

致命错误:内存不足(分配1611137024)(试图分配 1610350592 字节)在 /home/twitcast/public_html/system/index.php 上 第 121 行

正在运行的部分代码如下

function podcast()
  {
            $fetch = new server();
            $fetch->connect("TCaster");
            $collection = $fetch->db->shows;

            // find everything in the collection
            $cursor = $collection->find();

            if($cursor->count() > 0)
            {
                $test = array();
                // iterate through the results
                while( $cursor->hasNext() ) {   
                    $test[] = ($cursor->getNext());
                }
                $i = 0;
                foreach($test as $d) {

                for ( $i = 0; $i <= 3; $i ++) {
                $url = $d["streams"][$i];   
                $xml = file_get_contents( $url );
                $doc = new DOMDocument();
                $doc->preserveWhiteSpace = false;
                $doc->loadXML( $xml); // $xml = file_get_contents( "http://www.c3carlingford.org.au/podcast/C3CiTunesFeed.xml")

                // Initialize XPath    
                $xpath = new DOMXpath( $doc);
                // Register the itunes namespace
                $xpath->registerNamespace( 'itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd');

                $items = $doc->getElementsByTagName('item');    
                    foreach( $items as $item) {
                        $title = $xpath->query( 'title', $item)->item(0)->nodeValue;
                        $published = strtotime($xpath->query( 'pubDate', $item)->item(0)->nodeValue);
                        $author = $xpath->query( 'itunes:author', $item)->item(0)->nodeValue;
                        $summary = $xpath->query( 'itunes:summary', $item)->item(0)->nodeValue;
                        $enclosure = $xpath->query( 'enclosure', $item)->item(0);
                        $url = $enclosure->attributes->getNamedItem('url')->value;

                    $fname = basename($url);
                    $collection = $fetch->db->shows_episodes;

                    $cursorfind = $collection->find(array("internal_url"=>"http://twitcatcher.russellharrower.com/videos/$fname"));
                    if($cursorfind->count() < 1)
                    {


                        $copydir = "/home/twt/public_html/videos/";
                        $data = file_get_contents($url);
                        $file = fopen($copydir . $fname, "w+");

                        fputs($file, $data);

                        fclose($file);
                        $collection->insert(array("show_id"=> new MongoId($d["_id"]),"stream"=>$i,"episode_title"=>$title, "episode_summary"=>$summary,"published"=>$published,"internal_url"=>"http://twitcatcher.russellharrower.com/videos/$fname"));

                        echo "$title <br> $published <br> $summary <br> $url<br><br>\n\n";
                    }




                }

            }
            }
            }

第 121 行是

$data = file_get_contents($url);

【问题讨论】:

  • @khanahk $data = file_get_contents($url);

标签: php memory timeout


【解决方案1】:

您想为单个 PHP 线程增加 1.6GB 的内存使用量?虽然您可以增加内存限制,但我强烈建议您考虑另一种方式来做您想做的事。

可能是最简单的解决方案:您可以使用 CURL 请求源文件的字节范围(对于远程文件,无论如何使用 Curl 比 get_file_contents 更明智)。您可以一次获得 100K,写入本地文件,然后获得下一个 100k 并追加到文件等,直到整个文件被拉入。

您也可以使用流做一些事情,但它会变得更复杂一些。如果远程服务器不允许您按字节获取文件的一部分,这可能是您唯一的选择。

如果你的服务器有权限,最后还有诸如 wget 之类的 Linux 命令,通过 exec() 运行。

【讨论】:

    【解决方案2】:

    Memory Limit - 看看这个指令。假设这就是你所需要的。

    或者您可以尝试使用copy 而不是将文件读取到内存(据我了解,这是视频文件,因此占用大量内存并不奇怪):

    $copydir = "/home/twt/public_html/videos/";
    copy($url, $copydir . $fname);
    

    看起来昨晚打开的文件变小了)

    【讨论】:

    • 奇怪,因为它们都是高清文件,我会试一试,看看会发生什么,atm 我正在尝试 wget
    • @RussellHarrower 假设复制工作足够好。或 wget。基本上,任何不会将文件读入内存的东西。我只是将这些字节翻译成人类可读的东西,看起来你正在读取一个超过 1.6GB 的文件 - 将整个文件放入内存中(file_get_contents 这样做)不是你所需要的)
    猜你喜欢
    • 2014-06-01
    • 2019-03-18
    • 2014-01-25
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多