Javascript required
Skip to content Skip to sidebar Skip to footer

Php Read File Line by Line Into Array and Name Lines

How Can I Read a Large File Line by Line in PHP?

Every now and then, y'all volition have to read and process a large file in PHP. If yous are not conscientious when reading a large file, you will often cease up exhausting all the memory of your server. Even so, there is no need to worry because PHP already provides a lot of inbuilt functions to read large files (past large I mean files with size over 1GB) either line past line or 1 clamper at a time. In this article, yous will acquire about the pros and cons of all these techniques.

On This Page
  1. Reading a File Line by Line into an Array Using file()
  2. Reading a Large File One Line at a Time Using fgets()
  3. Reading a Large File in Smaller Pieces Using fread()
  4. Quick Summary

Reading a File Line by Line into an Array Using file()

You can use the file() function in PHP to read an entire file into an array. This function stores each line along with the newline grapheme in its own array element. If you intended to read a file line by line and shop the effect in an array, the file() function seems to a natural choice.

I major drawback of this method is that it will read the whole file at once. In other words, information technology solves the problem of reading a file one line at a time but it all the same reads the whole file at once. This means that you won't exist able to utilize it to read very large files.

PHP

            $life_of_bee_lines = file('life-of-the-bee.txt'); $short_lines = 0;  // Output — Total Lines: 6305. echo "Total Lines: ".count($life_of_bee_lines).".\n";  foreach($life_of_bee_lines as $line) {   if(strlen($line) <= 30) {     $short_lines++;   } }  // Output — Total number of short lines: 778. echo "Total number of short lines: $short_lines.\due north";                      

In the case in a higher place, I accept read a text file chosen The Life of the Bee from Project Gutenberg. This file is only 360kb long and then reading it using the file() role was not a problem. At present we will acquire how to read a file that might be over 1GB in size without exhausting our memory.

Reading a Big File Ane Line at a Time Using fgets()

The fgets() function in PHP reads the current line from an open file pointed to by the resource handle. The office stops reading after reaching a specified length, encountering a new line or reaching the terminate of file. It also returns FALSE if there is no more than data to be read. This ways that nosotros tin can check if a file has been read completely by comparison the return value of fgets() to false.

If you have not specified a detail length that this function should read, it will read the whole line. In other words, the maximum retention that y'all need with fgets() depends on the longest line in the file. Unless the file that you are reading has very long lines, you won't exhaust your memory similar you could with the file() role in previous section.

PHP

            $handle = fopen("life-of-the-bee.txt", "r"); $total_lines = 0; $short_lines = 0;  if ($handle) {     $line = fgets($handle);     while ($line !== imitation) {         $total_lines++;         if(strlen($line) <= thirty) {           $short_lines++;         }         $line = fgets($handle);     }     fclose($handle); }  // Output — Total Lines: 6305. repeat "Total Lines: $total_lines.\n";  // Output — Total number of brusque lines: 778. echo "Full number of short lines: $short_lines.\n";                      

Inside the if block, nosotros read the first line of our file and if information technology is not strictly equal to false we enter the while loop and process the whole file one line at a time. Nosotros could also combine the file reading and checking process in 1 line like I have done the following example.

PHP

            $handle = fopen("life-of-the-bee.txt", "r"); $total_lines = 0; $short_lines = 0;   if ($handle) {     while (($line = fgets($handle)) !== false) {         $total_lines++;         if(strlen($line) <= xxx) {           $short_lines++;         }     }     fclose($handle); }  // Output — Full Lines: 6305. repeat "Total Lines: $total_lines.\northward";  // Output — Total number of brusk lines: 778. echo "Total number of curt lines: $short_lines.\n";                      

I would like to bespeak out the just similar file(), the fgets() function reads the new line character besides. You tin can verify this by using var_dump().

Reading a Large File in Smaller Pieces Using fread()

One drawback of reading files using fgets() is that y'all will need substantial amount of memory to read files which have very long lines. The situation is very unlikely to happen but if you can't make any assumptions virtually the file it is improve to read it in modest sized chunks.

This is where the fread() office in PHP comes to our rescue. This function reads a file up to length number of bytes. Youare the one who gets to specify the length and so y'all don't take to worry near running out of memory.

PHP

            $handle = fopen("large-story.txt", "r"); $chunk_size = 1024*1024; $iterations = 0;   if ($handle) {     while (!feof($handle)) {         $iterations++;         $chunk = fread($handle, $chunk_size);     }     fclose($handle); }  // Output — Read the whole file in thirteen iterations. repeat "Read the whole file in $iterations iterations.";                      

In the in a higher place example, we read the large text file (1024*1024) 1MB at a time and it took 13 iterations to read the whole file. You can set the value of chunk_size to a reasonable value and read a very large file without exhausting your retentivity. Yous could use this technique to read a 1GB file using just 1MB of retentivity in about 1024 iterations. You can likewise read fifty-fifty larger files and increment the chunk_size if you want to keep the number of iterations low.

Quick Summary

Permit's recap everything that we take covered in this tutorial.

  1. If you desire to read files line by line into an array, using the file() function would be the right choice. The merely drawback if that this function will read whole file at once so you won't be able to read very big files without exhausting the retention.

  2. If the files you are reading are very big and you accept no plans to store the individual lines in an assortment, using the fgets() part would be the correct choice. The amount of memory that your script needs volition only depend on the length of largest line in the file that you are reading. This means that the file could be over 1GB in size simply if the private lines are non very large, you lot will never exhaust your retention.

  3. If you are reading big files and have no thought how long individual lines could be in that particular file, using fread() would be your best bet. This function lets you specify the size of chunks in which you want to read the file. Some other advantage of fread() over fgets() is that yous will not take to perform a lot of iterations if the file you are reading contains big number of empty lines.

Allow me know if there is anything that you would like me to clarify. Too, you lot are more than welcome to annotate if you know other techniques for reading large files 1 line at a time in PHP.

Charge per unit this post —

Loading...

barringtononewalre.blogspot.com

Source: https://tutorialio.com/read-a-large-file-line-by-line-in-php/