In PHP, file reading is a common task, and the fgets() function allows us to read data from a file line by line. This function is especially useful when working with large text files. In this article, we will explain how to use the fgets() function, providing practical code examples to help readers better understand its functionality.
The basic syntax of the fgets() function is as follows:
string fgets ( resource $handle, int $length );
Here’s an explanation of the parameters:
Assume we have a text file called example.txt with the following content:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
We can use the following code to read the file line by line and display its content:
$handle = fopen("example.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line . "<br>";
}
fclose($handle);
} else {
echo "Unable to open file";
}
This code first opens the example.txt file using the fopen() function and stores the returned file resource in the $handle variable. Then, inside a while loop, it uses the fgets() function to read each line, and it outputs each line to the page. Finally, the fclose() function is used to close the file.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
In some cases, you might only need to read a portion of each line. The second parameter, $length, of the fgets() function allows us to limit the number of bytes read. The following example shows how to read the first 10 bytes of each line:
$handle = fopen("example.txt", "r");
if ($handle) {
while (($line = fgets($handle, 10)) !== false) {
echo $line . "<br>";
}
fclose($handle);
} else {
echo "Unable to open file";
}
This code specifies 10 as the second parameter of fgets(), meaning it will read only the first 10 bytes of each line. The output will be as follows:
Lorem ipsu
consectetu
sed do
ut labore
In addition to directly outputting the file's content, we can further process each line of data. For example, we can count the number of characters in each line and store the result in an array. Here’s an example of how to do this:
$handle = fopen("example.txt", "r");
$result = [];
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lineLength = strlen($line);
$result[] = $lineLength;
}
fclose($handle);
} else {
echo "Unable to open file";
}
print_r($result);
In this example, we use the strlen() function to calculate the number of characters in each line and store the results in the $result array. Finally, we output the array contents:
Array
(
[0] => 28
[1] => 28
[2] => 30
[3] => 24
)
The fgets() function is a highly useful PHP function that allows us to read data line by line from a file. It is easy to understand and use, and can be combined with other functions to process the data as needed. Mastering fgets() will help you handle large text files efficiently and perform a variety of file reading tasks with ease.