How to Use Fgets to Read Line by Line

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

Every now and and so, you volition have to read and process a large file in PHP. If you are not conscientious when reading a large file, you will often terminate up exhausting all the memory of your server. However, at that place is no need to worry because PHP already provides a lot of inbuilt functions to read big files (by large I mean files with size over 1GB) either line by line or one clamper at a time. In this article, you will learn nearly 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 Big File One Line at a Time Using fgets()
  3. Reading a Big File in Smaller Pieces Using fread()
  4. Quick Summary

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

You can employ the file() part in PHP to read an entire file into an array. This part stores each line along with the newline character in its own array element. If yous intended to read a file line by line and store the issue in an assortment, the file() role seems to a natural option.

One major drawback of this method is that it volition read the whole file at once. In other words, it solves the problem of reading a file one line at a time merely it still reads the whole file at once. This means that y'all won't be able to utilize it to read very big files.

PHP

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

In the example above, I have read a text file chosen The Life of the Bee from Projection Gutenberg. This file is just 360kb long and so reading it using the file() function was not a problem. At present we will learn how to read a file that might exist over 1GB in size without exhausting our memory.

Reading a Big File One Line at a Fourth dimension Using fgets()

The fgets() function in PHP reads the electric current line from an open up file pointed to past the resource handle. The function stops reading after reaching a specified length, encountering a new line or reaching the end of file. It likewise returns FALSE if in that location is no more than data to be read. This means that we can check if a file has been read completely past comparing the return value of fgets() to faux.

If y'all have not specified a detail length that this function should read, it volition read the whole line. In other words, the maximum memory 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 like you could with the file() function in previous department.

PHP

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

Inside the if block, we read the beginning line of our file and if it is not strictly equal to false nosotros enter the while loop and procedure the whole file one line at a fourth dimension. We could too combine the file reading and checking procedure in one line like I have washed 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) <= thirty) {           $short_lines++;         }     }     fclose($handle); }  // Output — Total Lines: 6305. repeat "Total Lines: $total_lines.\due north";  // Output — Full number of short lines: 778. echo "Total number of short lines: $short_lines.\n";                      

I would similar to point out the merely like file(), the fgets() function reads the new line character besides. Y'all tin verify this by using var_dump().

Reading a Big File in Smaller Pieces Using fread()

1 drawback of reading files using fgets() is that you volition need substantial amount of retention to read files which accept very long lines. The situation is very unlikely to happen but if you tin't brand any assumptions about the file it is better to read it in small-scale sized chunks.

This is where the fread() function 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 then you don't accept to worry nearly running out of retentiveness.

PHP

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

In the above case, we read the large text file (1024*1024) 1MB at a time and it took 13 iterations to read the whole file. Yous tin can gear up the value of chunk_size to a reasonable value and read a very large file without exhausting your memory. You lot could use this technique to read a 1GB file using just 1MB of retentiveness in nigh 1024 iterations. Yous tin also read even larger files and increment the chunk_size if y'all want to keep the number of iterations low.

Quick Summary

Permit'southward recap everything that we accept covered in this tutorial.

  1. If y'all want to read files line by line into an array, using the file() function would be the correct choice. The only drawback if that this function will read whole file at once and so you won't exist able to read very big files without exhausting the memory.

  2. If the files you are reading are very big and you have no plans to store the individual lines in an array, using the fgets() function would be the right option. The corporeality of retentiveness that your script needs will only depend on the length of largest line in the file that you are reading. This means that the file could exist over 1GB in size simply if the individual lines are not very large, you will never exhaust your memory.

  3. If y'all are reading big files and take no idea how long individual lines could exist in that item file, using fread() would exist 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 y'all volition not have to perform a lot of iterations if the file y'all are reading contains large number of empty lines.

Let me know if there is annihilation that you lot would similar me to analyze. Also, yous are more than welcome to comment if y'all know other techniques for reading large files 1 line at a fourth dimension in PHP.

Rate this post —

Loading...

ohearnwourease.blogspot.com

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

0 Response to "How to Use Fgets to Read Line by Line"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel