应该一行一行进行处理,不能一次加载到内存。
1 $line = '';
2
3 $f = fopen('data.txt', 'r');
4 $cursor = -1;
5
6 fseek($f, $cursor, SEEK_END);
7 $char = fgetc($f);
8
9 /**
10 * Trim trailing newline chars of the file
11 */
12 while ($char === "\n" || $char === "\r") {
13 fseek($f, $cursor--, SEEK_END);
14 $char = fgetc($f);
15 }
16
17 /**
18 * Read until the start of file or first newline char
19 */
20 while ($char !== false && $char !== "\n" && $char !== "\r") {
21 /**
22 * Prepend the new char
23 */
24 $line = $char . $line;
25 fseek($f, $cursor--, SEEK_END);
26 $char = fgetc($f);
27 }
28
29 echo $line;
本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。