读取文件内容的最快方法 [英] Fastest way possible to read contents of a file

查看:144
本文介绍了读取文件内容的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在寻找最快的方法来通过服务器上的文件路径通过PHP读取文件的所有内容,这些文件也可能是巨大的。所以它是非常重要的,它只是尽快读取它。



它是逐行阅读比阅读整个内容更快?虽然,我记得有些读了这个,读完整个内容会导致大文件的错误。这是真的吗?

解决方案

如果要将文件的全部内容加载到PHP变量, >(可能最快)方式是 file_get_contents

但是,如果您正在处理大文件,则将整个文件加载到内存中可能不是这样的好主意:你可能最终得到一个 <错误,因为PHP不允许您的脚本使用超过(通常)一对夫妇巨型 -

所以,即使它不是最快的解决方案,按行读取文件(
fopen
+ fgets + fclose ),并且不需要将整个文件加载到记忆,可能是必要的...


Ok, I'm looking for the fastest possible way to read all of the contents of a file via php with a filepath on the server, also these files can be huge. So it's very important that it does a READ ONLY to it as fast as possible.

Is reading it line by line faster than reading the entire contents? Though, I remember reading up on this some, that reading the entire contents can produce errors for huge files. Is this true?

解决方案

If you want to load the full-content of a file to a PHP variable, the easiest (and, probably fastest) way would be file_get_contents.

But, if you are working with big files, loading the whole file into memory might not be such a good idea : you'll probably end up with a memory_limit error, as PHP will not allow your script to use more than (usually) a couple mega-bytes of memory.


So, even if it's not the fastest solution, reading the file line by line (fopen+fgets+fclose), and working with those lines on the fly, without loading the whole file into memory, might be necessary...

这篇关于读取文件内容的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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