Reading by large file line in php
When a large file in php is read as file_get_contents, it is loaded into memory at once, and a situation in which php script cannot be executed due to insufficient memory occurs.
At this time, if you read line by line, you can avoid running out of memory.
Below is an example code
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
} else {
// error opening the file.
}
Comments
Post a Comment