Reputation: 24635
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 PHP?
Upvotes: 9
Views: 15562
Reputation: 14873
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
Upvotes: 23
Reputation: 58
fgets($fileHandle)
is what you're looking for. You get the file handle using fopen("filename.txt", "r")
, and close it with fclose($fileHandle)
.
Upvotes: 4