newbie
newbie

Reputation: 24635

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 PHP?

Upvotes: 9

Views: 15562

Answers (2)

Pramendra Gupta
Pramendra Gupta

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

Rob Percival
Rob Percival

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

Related Questions