Reading huge file line by line in PHP
In Java I use Scanner to read text file line by line, so memory usage will be very low, because only one line is in memory once. Is there similar method in 开发者_如何学JAVAPHP?
use fseek, fgets
$handle = fopen("/tmp/uploadfile.txt", "r") or die("Couldn't get handle");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle);
// Process line here..
}
fclose($handle);
}
Reading very large files in PHP
fgets($fileHandle)
is what you're looking for. You get the file handle using fopen("filename.txt", "r")
, and close it with fclose($fileHandle)
.
精彩评论