How to read file content in php - PHP provides a number of functions for reading data from files. The methods used are also varied: one is read per line called the function fgets (), there is also a byte called fread (). Both fgets and fread functions have their own characteristics and can be used according to programming conditions.
Fungsi fgets()
Used to read one line of opened file. The fgets() function can return data line by line. The fgets() function stops returning a new row if the length parameter is specified on a specified value or at the end of the file (end of file) depending on which is reached first. If it fails, the function fgets() will return the FALSE value.
Fungsi fread()
Used to read the contents of the file being opened. The fread function returns a frequently readable value to the end of the file (end of file) or up to a predetermined length. The length size is expressed in bytes / characters. If it fails, the function fread() will return the FALSE value.
To be able to understand it better, here's an example of its application to a program.
Program 1
File Name: file04.php
Description: The program reads and displays the contents of the file.
Open notepad, then type the following php code.
<?php
$filename = "data.txt";
$handle = fopen ($filename, "r");
if (!$handle) {
echo "<b>File can not be opened or not yet exists</b>";
} else {
$content = fgets ($handle, 2048);
//$content2 = fread ($handle, 20);
echo "Isi 1 : $content<br>";
//echo "Isi 2 : $content2<br>";
}
fclose($handle);
?>
Save the code with file04.php name in htdocs folder.
Program Explanation 1
To read the contents of the file, can use the function fgets() and fread(). The fgets() function reads the contents of each line file. While the function fread() will read the contents of the file every number of bytes. In the above program, the 8th row will read the file every 20 bytes or 20 characters.
To run the program, open the browser, type http: //localhost/file04.php in the address bar then enter. The result will be like this.
Program 2
File Name: file05.php
Description: The program reads the contents of the line file each line.
<?php
$filename = "data.txt";
$handle = fopen ($filename, "r");
if (!$handle) {
echo "<b>File can not be opened or not yet exists</b>";
} else {
echo "<b>Fill the file : </b><br>";
while ($content = fgets ($handle, 2048)) {
echo "$content<br>";
}
}
fclose($handle);
?>
Program Explanation 2
Program 2 above will display the entire contents of the file, where the contents of the file will be read each line. The reading is done from the first line to the last line.
Program 3
File Name: file06.php
Description: The program reads the contents of the line file each line.
<?php
$filename = "data.txt";
$handle = @fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer."<br>";
}
fclose($handle);
}
?>
Program Explanation 3
Program 3 above will display the entire contents of the file, where the contents of the file will be read each line. The readings are made as long as the file pointer has not reached the end-of-file. The feof() function on line 5 serves to check the file pointer is at the end of the file or not.
That's my tutorial on How to read content file in php. Hope can improve your ability in studying php programming language. Also read articles about Write to Text File in PHP.
Fungsi fgets()
Used to read one line of opened file. The fgets() function can return data line by line. The fgets() function stops returning a new row if the length parameter is specified on a specified value or at the end of the file (end of file) depending on which is reached first. If it fails, the function fgets() will return the FALSE value.
Fungsi fread()
Used to read the contents of the file being opened. The fread function returns a frequently readable value to the end of the file (end of file) or up to a predetermined length. The length size is expressed in bytes / characters. If it fails, the function fread() will return the FALSE value.
To be able to understand it better, here's an example of its application to a program.
Program 1
File Name: file04.php
Description: The program reads and displays the contents of the file.
Open notepad, then type the following php code.
<?php
$filename = "data.txt";
$handle = fopen ($filename, "r");
if (!$handle) {
echo "<b>File can not be opened or not yet exists</b>";
} else {
$content = fgets ($handle, 2048);
//$content2 = fread ($handle, 20);
echo "Isi 1 : $content<br>";
//echo "Isi 2 : $content2<br>";
}
fclose($handle);
?>
Save the code with file04.php name in htdocs folder.
Program Explanation 1
To read the contents of the file, can use the function fgets() and fread(). The fgets() function reads the contents of each line file. While the function fread() will read the contents of the file every number of bytes. In the above program, the 8th row will read the file every 20 bytes or 20 characters.
To run the program, open the browser, type http: //localhost/file04.php in the address bar then enter. The result will be like this.
![]() |
Program view |
Program 2
File Name: file05.php
Description: The program reads the contents of the line file each line.
<?php
$filename = "data.txt";
$handle = fopen ($filename, "r");
if (!$handle) {
echo "<b>File can not be opened or not yet exists</b>";
} else {
echo "<b>Fill the file : </b><br>";
while ($content = fgets ($handle, 2048)) {
echo "$content<br>";
}
}
fclose($handle);
?>
Program Explanation 2
Program 2 above will display the entire contents of the file, where the contents of the file will be read each line. The reading is done from the first line to the last line.
![]() |
Program view |
Program 3
File Name: file06.php
Description: The program reads the contents of the line file each line.
<?php
$filename = "data.txt";
$handle = @fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer."<br>";
}
fclose($handle);
}
?>
Program Explanation 3
Program 3 above will display the entire contents of the file, where the contents of the file will be read each line. The readings are made as long as the file pointer has not reached the end-of-file. The feof() function on line 5 serves to check the file pointer is at the end of the file or not.
![]() |
Program view |
That's my tutorial on How to read content file in php. Hope can improve your ability in studying php programming language. Also read articles about Write to Text File in PHP.
Comments
Post a Comment