All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Read File Line By Line

Last Updated : Sep 14, 2023

PHP Read File Line By Line

In this article we will show you the solution of PHP read file line by line, in PHP, you can read a file line by line by iterating through each line in the file.

This can be accomplished by opening the file with the fopen() function in the ‘r’ mode and then reading each line of the file with the fgets() function inside a loop until the feof() function returns true.

The fgets() function reads one line at a time from the file and moves the file reference to the next line.

This method allows you to process huge files without having to load the entire file into memory, making it useful for dealing with enormous datasets.

Now move to the concept of php read file line by line.

Step By Step Guide On PHP Read File Line By Line :-

Method - Using file() to Read the File

<?php
$lines = file('demo.txt');
$count = 0;
foreach($lines as $line) {
    $count += 1;
    echo str_pad($count, 2, 0, STR_PAD_LEFT).". ".$line;
}
?>
  1. As you can see, we use the file() function to build PHP code that reads a file line by line.
  2. Reading through demo.txt text file and assigning an array of lines to a variable named $lines is done using this PHP code's implementation that utilizes the file() method.
  3. Then we set a variable named $count to 0 and use a for each loop to loop through each line in the $lines array.
  4. It increments the $count variable by one for each line, padding the $count value with a leading 0 to guarantee that it is always two digits long.
  5. Afterward it publishes the line number preceded by the line content to standard output using the echo command.
  6. The result of this code would be a numerical listing of lines from the "demo.txt" file, with each line preceded by its matching line number in two-digit format.
  7. The goal of this code could be to add line numbers to the file's text for better reference and readability.

Method - Using fgets() to Read the File

<?php
$file_handle = fopen('demo.txt', 'r');
function get_all_lines($file_handle) {
    while (!feof($file_handle)) {
        yield fgets($file_handle);
    }
}
$count = 0;
foreach (get_all_lines($file_handle) as $line) {
    $count += 1;
    echo $count.". ".$line;
}
fclose($file_handle);
?>
  1. In order to achieve reading a file row-by-row similar to this current example here, we utilized PHP along with fgets () in our coding.
  2. This PHP code reads the contents of the text file "demo.txt" using a file handle and a custom function called get_all_lines().
  3. The fopen() function is used at the beginning of the code to open the file in read mode and construct a file handle named $file_handle.
  4. While using feof() function to detect end of file condition in a while loop which iterates through all lines in the textfile - this defines how get_all_lines() works.
  5. It yields the line content for each line using the yield keyword, which returns the line content to the foreach loop that executes this function.
  6. The foreach loop then loops through the file, invoking the get_all_lines() function and using the given value for each iteration.
  7. It increases the $count variable by one for each line and then uses the echo statement to print the line number followed by the line content to standard output.
  8. To close the file handle, we utilise the fclose() function at the end.

Conclusion :-

As a result, we have mastered the notion of PHP read file line by line.

We also discovered that reading files in PHP is a popular activity with a variety of ways.

The file() and file handles functions are two standard techniques for reading files, whereas the fgets() function can read files line by line.

To add line numbers to a file, use the str_pad() function to format the numbers, and loops like foreach or while to cycle through the file contents.

I hope this article on PHP read file line by line helps you and the steps and method mentioned above are easy to follow and implement.