1. Code
  2. Coding Fundamentals

Read a File Line by Line With PHP

Scroll to top
6 min read
11

There are two reasons why you might want to read a file line by line with PHP:

  1. The project you are working on requires you to process the file one line at a time.
  2. You are reading a very large file, and the only way to read it without going over the memory limit is to do it one line at a time.

Using file() to Read the File

You can use the file() function in PHP to read the entire file at once into an array. The array elements are individual lines of the file. So you will be able to iterate over the lines in the file by iterating over the array. The function accepts three parameters:

  • filename: This is the file that you want to read. You can also supply a URL as the filename.
  • flags: This is an optional parameter and can be set to one or more of the following constant values: FILE_USE_INCLUDE_PATH, FILE_IGNORE_NEW_LINES, and FILE_SKIP_EMPTY_LINES.
  • context: This is also an optional parameter used to modify the behavior of a stream.

We will be using the FILE_SKIP_EMPTY_LINES flag to skip all the empty lines in a file. You might also want to use FILE_IGNORE_NEW_LINES to remove the line endings from individual lines.

This function returns an array with the file contents upon success and false upon failure. You will also get an E_WARNING level error if the file does not exist. Here is an example of using this function.

1
<?php
2
3
$lines = file('pride-and-prejudice.txt');
4
$count = 0;
5
6
foreach($lines as $line) {
7
    $count += 1;
8
    echo str_pad($count, 2, 0, STR_PAD_LEFT).". ".$line;
9
}
10
11
?>

The output of the above code looks like this:

1
01. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen
2
02. 
3
03. This eBook is for the use of anyone anywhere in the United States and
4
04. most other parts of the world at no cost and with almost no restrictions
5
05. whatsoever. You may copy it, give it away or re-use it under the terms
6
06. of the Project Gutenberg License included with this eBook or online at
7
07. www.gutenberg.org. If you are not located in the United States, you
8
08. will have to check the laws of the country where you are located before
9
09. using this eBook.
10
10. 
11
11. Title: Pride and Prejudice
12
12. 
13
13. Author: Jane Austen
14
14. 
15
15. Release Date: June, 1998
16
16. [Most recently updated: August 23, 2021]

You can see that there are some empty lines in the output; we can get rid of them by using the FILE_SKIP_EMPTY_LINES flag. Also, it might not be evident, but the lines above include the newline character. That is why we did not have to add a newline character of our own when echoing the lines. You can get rid of the blank lines by using the FILE_IGNORE_NEW_LINES flag.

1
<?php
2
3
$lines = file('pride-and-prejudice.txt', FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
4
$count = 0;
5
6
foreach($lines as $line) {
7
    $count += 1;
8
    echo str_pad($count, 2, 0, STR_PAD_LEFT).". ".$line;
9
}
10
11
?>

The output with those flags will look like this:

1
01. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen 02. This eBook is for the use of anyone anywhere in the United States and 03. most other parts of the world at no cost and with almost no restrictions 04. whatsoever. You may copy it, give it away or re-use it under the terms 05. of the Project Gutenberg License included with this eBook or online at 06. www.gutenberg.org. If you are not located in the United States, you 07. will have to check the laws of the country where you are located before 08. using this eBook. 09. Title: Pride and Prejudice 10. Author: Jane Austen 11. Release Date: June, 1998 [eBook #1342] 12. [Most recently updated: August 23, 2021] 

Using the file() function is an easy way to read a file line by line in PHP if you are not worried about memory usage. However, you will have to get more creative if memory usage is an issue because file() reads the entire file into an array at once.

Using fgets() to Read the File

Another way to read a file line by line with PHP is to use the fgets() function. It has one required parameter, which is a valid file handle. We will use the fopen() function to get access to the file handle. Here is the code that we are going to run:

1
<?php
2
3
$file_handle = fopen('pride-and-prejudice.txt', 'r');
4
5
function get_all_lines($file_handle) { 
6
    while (!feof($file_handle)) {
7
        yield fgets($file_handle);
8
    }
9
}
10
11
$count = 0;
12
13
foreach (get_all_lines($file_handle) as $line) {
14
    $count += 1;
15
    echo $count.". ".$line;
16
}
17
18
fclose($file_handle);
19
20
?>

On the first line, we open our file in read-only mode. Then, we define a function which accepts a $file_handle as a parameter and gives back a single line. Please note that we are using a yield statement, and our function get_all_lines() is a generator function. You might want to read about generator functions in PHP if you haven't used them before.

We are using the feof() function inside get_all_lines() to check whether our file pointer has reached the end of the file. We only yield as long as we are not at the end of the file. You should get the following output by running the code above:

1
1. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen
2
2. 
3
3. This eBook is for the use of anyone anywhere in the United States and
4
4. most other parts of the world at no cost and with almost no restrictions
5
5. whatsoever. You may copy it, give it away or re-use it under the terms
6
6. of the Project Gutenberg License included with this eBook or online at
7
7. www.gutenberg.org. If you are not located in the United States, you
8
8. will have to check the laws of the country where you are located before
9
9. using this eBook.
10
10. 
11
11. Title: Pride and Prejudice
12
12. 
13
13. Author: Jane Austen
14
14. 
15
15. Release Date: June, 1998 
16
16. [Most recently updated: August 23, 2021]

The output looks the same as in our previous section. The only difference this time is that you are no longer in danger of running out of memory.

I mentioned earlier that fgets() will allow you to read one line of the file at a time, and it only requires a single parameter that points to the file pointer for the file that you want to read. The memory consumption in this case would depend on the length of the line, and there is little chance of you running out of memory.

However, let's say you're reading a text file which contains unusually long lines. You can then pass an optional second parameter to the fgets() function which specifies the number of characters that you want to read. It will then read length - 1 bytes from the file before stopping. It will stop earlier if it comes across a new line or the end of the file. This gives you more control over the memory consumption of your code.

Final Thoughts

I have discussed two methods of reading a file line by line with PHP in this tutorial. There are a couple more ways of doing this, but these two will meet almost all your needs. Use the file() function when memory consumption is not an issue, and use fgets() with a generator function if you want to conserve memory.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.