加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 读取和解析非常大的文件的内容

发布时间:2020-12-13 18:04:41 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Least memory intensive way to read a file in PHP4个 我试图解析一个大小约1GB的制表符分隔文件. 在哪里运行脚本我得到: Fatal error: Allowed memory size of 1895825408 bytes exhausted (tried to allocate 1029206974 bytes) ... 我目前
参见英文答案 > Least memory intensive way to read a file in PHP4个
我试图解析一个大小约1GB的制表符分隔文件.

在哪里运行脚本我得到:

Fatal error: Allowed memory size of 1895825408 bytes exhausted  (tried to allocate 1029206974 bytes) ...

我目前的脚本只是:

$file = file_get_contents('allCountries.txt') ;

$file = str_replace(array("rn","t"),array("[NEW*LINE]","[tAbul*Ator]"),$file) ;

我已经将php.ini中的内存限制设置为-1,然后给了我:

Fatal error: Out of memory (allocated 1029963776) (tried to allocate 1029206974 bytes)

是否有部分打开文件,然后继续下一部分,以便一次用尽更少的内存?

是的,你可以逐行阅读:
$handle = @fopen("/tmp/inputfile.txt","r");
if ($handle) {
    while (($buffer = fgets($handle,4096)) !== false) {
        echo $buffer;
    }
    fclose($handle);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读