致命错误:PHP内存不足


Fatal error: Out of memory PHP

我不知道为什么昨晚一切顺利,而今天早上我得到了

致命错误:内存不足(已分配1611137024)(试图分配1610350592字节)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);

您想为单个PHP线程增加1.6GB的内存使用量吗?虽然你可以提高记忆力,但我强烈建议你换一种方式做你想做的事。

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

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

最后还有一些Linux命令,比如wget,如果您的服务器有权限,可以通过exec()运行。

内存限制-看看这个指令。假设这就是你所需要的。

或者你可以尝试使用copy而不是将文件读取到内存中(据我所知,这是视频文件,所以它占用大量内存并不奇怪):

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

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