Saturday, July 30, 2022

Read a File Line by Line With PHP

Read a File Line by Line With PHP

There are two reasons why you might want to read a file line by line with PHP. First one is that the project you are working on requires you to process the file one line at a time. Second is that 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 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 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.

The output of the above code looks like this:

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 while echoing the lines. You can get rid of the blank lines by using the FILE_IGNORE_NEW_LINES flag.

The output with those flags will look like this:

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:

On the first line, we open our file in read-only mode. Then, we define a function which accepts a $file_handle as 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 end-of-file. We only yield as long as we are not at the end of file. You should get the following output by running the code above:

The outputs looks the same as 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 a little chance of you running out of memory.

However, lets say you are 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 newline or end of 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.


No comments:

Post a Comment