PHP:fgets 相当于从文件的_底部_读取?
我有一个通过附加来更新自身的文件,但是该文件变得很大(500MB)。我想读取文件中的最后 50 行。这怎么能做到呢?
I have a file that updates itself by appending, however this file becomes huge (500MB). I would like to read the last 50 lines in the file. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tail -n50 将返回文件的最后 50 行。
因此,您不必将整个文件加载到内存中。
编辑:
如果您想在每行之后回显“
”:Tail -n50 will return the last 50 lines of the file.
Therefore, you don't have to load the entire file in memory.
Edit:
If you want to echo "
<br>
" after each line you do:您需要使用
fseek
将文件指针从文件末尾移动一定数量的字节:您必须告诉
fgets
有多少您想要读取的字节数,而不是多少行。它不知道文件的格式是什么。您必须将结果拆分为换行符并查看是否有 50 行。You'll need to use
fseek
to move the file pointer a certain number of bytes from the end of the file:You'll have to tell
fgets
how many bytes you want to read, not how many lines. It has no idea what the format of the file is. You'll have to split the result on a newline and see if you have 50 lines.