读取文件内容的最快方法 [英] Fastest way possible to read contents of a file
问题描述
它是逐行阅读比阅读整个内容更快?虽然,我记得有些读了这个,读完整个内容会导致大文件的错误。这是真的吗?
如果要将文件的全部内容加载到PHP变量, >(可能最快)方式是 但是,如果您正在处理大文件,则将整个文件加载到内存中可能不是这样的好主意:你可能最终得到一个 <错误,因为PHP不允许您的脚本使用超过(通常)一对夫妇巨型 - file_get_contents
+
所以,即使它不是最快的解决方案,按行读取文件( 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屋!