There are many ways to read files in the php language. Today we are going to talk about three php examples of reading text files, mainly using the fopen, file, file_get_contents functions. There are many ways to read files in the php language. Today we are going to talk about three php examples of reading text files, mainly using the fopen, file, file_get_contents functions.
Script ec (2); script
// Fopen File Reading instance
| The Code is as follows: |
|
|
$ Path named 'a.txt ';
$ Fp = fopen ($ file, "r"); // open the file in read-only mode
While (! (Feof ($ fp )))
{
$ Text = fgets ($ fp); // read a row of the object
Echo $ text;
} |
// File_get_contents reads the object
| The Code is as follows: |
|
|
If (file_exists ($ path ))
{
$ Body = file_get_contents ($ path );
Echo $ body; // Input File Content
}
Else
{
Echo "the file does not exist $ path ";
}
// File |
Read text files
| The Code is as follows: |
|
| $ Cbody = file ($ path ); |
Print_r ($ cbody); // because the files read by file are saved as arrays, print_r is used for output.