BlogProgramming

PHP: How to read a text file line by line

Had to read a file line by line in php, without completely loading it in memory.
The file is way too big to open only in memory, so I was getting memory exhaust errors.

This can be solved by using fgets() function to read the file line by line:

$handle = fopen("myfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // the line read
    }
    fclose($handle);
} else {
    // open file error
}

Did this worked out for you? Post your comments below.

Related Articles

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.

Back to top button
João Clérigo - Photography
Close

AdBlocker Detetado
AdBlocker Detected

Por favor ajude este website permitindo a visualização de alguns anúncios. Obrigado. Please help this website allowing the view of some advertising. Thank you!