Although we have database to work with data, with some circumstances we still need to work directly with files, especially text files. Here in this tutorial, you will know how to read a text file using PHP with a few different functions including fgets(), file(), and file_get_contents().
This tutorial will introduce methods to read text files line by line using PHP.
To read text files line by line in PHP, use the fgets() function and a while loop. If there is a line, the function returns it; if there are no more lines to read, it returns false. Two parameters are required. The following is the syntax.
fgets($file, $length)
In this case, $file resembles a file pointer that refers to an opened file. The number of bytes to be read is indicated by the optional $length argument.
Using the while loop and the fgets() method, we can cycle through each line after using the open() function to read the file. The following text can be found in the text file abc.txt.
Hi
How are you
Have a great day
Create a variable called $txt file and put the fopen() function in it as an example. In r mode, open the file abc.txt. Make a line counter variable called $a and give it the number 1. Make a while loop next. Write the fgets() function with $text file as the parameter inside the loop's parenthesis. In the loop, give a $line variable the function. Within the body of the loop, print the $line and $a variables together. Increase the $a variable and shut off the file stream using the fclose() method outside of the loop.
Example Code:
<?php
$txt_files = fopen('abc.txt','r');
$x = 1;
while ($line = fgets($txt_files)) {
echo($x." ".$line)."<br>";
$x++;
}
fclose($txt_files);
?>
Output:
1 Hi
2 How are you
3 Have a great day
The entire file is read into an array by the file() function. The file() method has the following syntax.
file($filename, $flag, $context)
The file path to be read in this case is $filename. Various constants, including FILE USE INCLUDE PATH, FILE IGNORE NEW LINES, and FILE SKIP EMPTY LINES, are included in the optional $flag option. The third one, which defines a context resource, is also optional.
If the file exists or returns false, the file() function returns the entire array. With the help of the foreach() function, we can utilize the function to read the file context line by line. The foreach() function iterates over the whole document, extracting each line one at a time.
Put the filepath, for instance, in the variable $txt file. Create the variable $lines and assign the $txt file parameter to the file() function. Then, go through the file's contents using the foreach() function. Use $num=>$line as the value and $lines as the iterator. Print the $num and $line variables inside the loop's body.
Example Code:
$txt_file = 'abc.txt';
$lines = file($txt_file);
foreach ($lines as $num=>$line){
echo 'Line '.$num.': '.$line.'<br/>';
}
Output:
Line 0: Hi
Line 1: How are you
Line 2: Have a great day
The entire file is read into a string via the file get contents() function. If the contents are present, it returns the entire file as a string; otherwise, it returns false. The function argument that we can supply is the file path. Using a separator, the explode() function divides a string into an array. The explode() method has the following syntax.
explode(separator, $string, $limit)
When returning the value, the separator option is used to divide the $string into the specified number of $limit pieces. To read a text file line by line in PHP, we can combine the file get contents(), explode(), and foreach() functions.
Create the variable $contents, for instance, and call the file get contents() method on it with the filepath as an input. Use the n character as a separator and $contents as the string when using the explode() method. Give the function to the $lines variable. $lines as $line should be looped using the foreach loop. Print the $line after that inside the loop body.
The $contents variable in the example below returns a string. With the n newline character, we divided it into an array, and then we used the loop to display each line.
Example Code:
$contents=file_get_contents("abc.txt");
$lines=explode("n",$contents);
foreach($lines as $line){
echo $line.'<br>';
}
Output:
Hi
How are you
Have a great day
That's it, hope it's helpful.