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?
PHP Read Single Line - fgets() The fgets() function is used to read a single line from a file.
PHP fgets() Function $file = fopen("test. txt","r");
PHP fgets() Function The fgets function is used to read php files line by line. It has the following basic syntax.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With