在脚本中打印图像的所有src的php脚本

时间:2011-08-15 12:20:35

标签: php memory-management

我正在使用Simple HTML DOM来检索网站所有图像的所有网址,但在执行过程中我遇到内存使用错误,如何解决?

$count = 0;
$last = 1721;
include('simple_html_dom.php');

while ( $count <= $last) { 

$html = file_get_html('http://myuri/?from='.$count);

// find all image inside post div
    foreach($html->find('div.itemPost img') as $e) {

        echo $e->src . '<br>';

    }

}

这是错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes) in /home/peppo1616/public_html/script/simple_html_dom.php on line 1189

1 个答案:

答案 0 :(得分:1)

在循环浏览img标记后调用析构函数,并将null指定给$html以清除一些内存。

while ( $count <= $last) { 
    $html = file_get_html('http://myuri/?from='.$count);

    // find all image inside post div
    foreach($html->find('div.itemPost img') as $e) {
        echo $e->src . '<br>';
    }

    $html->clear();
    $html = null;
}

在旁注中,我没有看到$count的任何增量,你可能会以无限循环结束。