猿问

如何逐行读取大文件?

如何逐行读取大文件?

我想逐行读取文件,但不需要将其完全加载到内存中。

我的文件太大,无法在内存中打开,如果尝试这样做,我总是能够避免内存错误。

文件大小为1GB。


慕神8447489
浏览 473回答 3
3回答

繁花如伊

您可以使用fgets()函数逐行读取文件:$handle = fopen("inputfile.txt", "r");if ($handle) {     while (($line = fgets($handle)) !== false) {         // process the line read.     }     fclose($handle);} else {     // error opening the file.}

MYYA

if ($file = fopen("file.txt", "r")) {     while(!feof($file)) {         $line = fgets($file);         # do same stuff with the $line     }     fclose($file);}
随时随地看视频慕课网APP
我要回答