在PHP中有效地逐行读取大文件的文件? [英] Reading a file line by line for large files efficiently in PHP?

查看:56
本文介绍了在PHP中有效地逐行读取大文件的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序读取了一个 5MB - 10MB 的大文件,该文件已通过 json 条目逐行输入.

My app reads a large file 5MB - 10MB that has been entered in with json entries line by line.

每一行都由一个解析器处理,该解析器被馈送到多个解析器并分别处理.一旦文件被读取,文件就会被移动.程序不断地收到要处理的文件.

Each line is handled by a parser that is fed to multiple parsers and treated separately. Once the file is read, the file is moved. The Program is continuously fed with files to be processed.

该程序当前使用 @file_get_contents($filename).程序的结构按原样工作.

The program currently works with @file_get_contents($filename). The program's structure works as is.

问题在于file_get_contents 将整个文件作为数组加载,整个系统运行一分钟.我怀疑如果我逐行读取它而不是等待文件加载到内存中,我可以提高速度(我可能是错误的并且愿意接受建议).

The problem is that file_get_contents loads the entire file as an array and the entire system runs for a minute. I suspect that I can gain speed if I read it line by line rather than wait for the file to load into memory (I might be wrong and open to suggestion).

执行此操作的文件处理程序太多了.实现我所需要的最有效方法是什么,哪种文件读取方法最适合这种情况?

There are too many file handler that does this. What is the most effective way to achieve what I need and which file reading method is best for this?

我有 fopenfreadreadfilefilefscanf与我的头顶相抗衡.但是,当我为他们阅读 man 时 - 它的所有代码都可以读取通用文件,而没有明确指示什么最适合大文件.

I have fopen, fread, readfile, file, and fscanf to contend with off the top of my head. However when I read the man for them - its all code to read generic files without a clear indication what is best for larger files.

推荐答案

$file = fopen("file.json", "r");

if ($file)
{
    while (($line = fgets($file)) !== false) 
    {
        echo $line;
    }
}
else 
{
    echo "Unable to open the file";
}

Fgets 读取直到到达 EOL 或 EOF.如果需要,您可以使用第二个参数添加它应该读取的内容.

Fgets read until it reach EOL, or EOF. if you want, you can add how much it should read using the second arg.

有关 fget 的更多信息:http://us3.php.net/fgets

For more info about fgets: http://us3.php.net/fgets

这篇关于在PHP中有效地逐行读取大文件的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆