This article mainly introduces three methods for php to read file content. you can refer to the following three methods for php to read file content:
// ************** First method of reading ******************** *********
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
// Determine whether the file exists
If (file_exists ($ file_path )){
If ($ fp = fopen ($ file_path, "a + ")){
// Read the file
$ Conn = fread ($ fp, filesize ($ file_path ));
// Replace the string
$ Conn = str_replace ("\ r \ n ","
", $ Conn );
Echo $ conn ."
";
} Else {
Echo "file cannot be opened ";
}
} Else {
Echo "this file does not exist ";
}
Fclose ($ fp );
// ******************* Second read method *************** ************
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
$ Conn = file_get_contents ($ file_path );
$ Conn = str_replace ("\ r \ n ","
", File_get_contents ($ file_path ));
Echo $ conn;
Fclose ($ fp );
// ******************** The third read method, cyclically read *****************
The code is as follows:
Header ("content-type: text/html; charset = utf-8 ");
// File path
$ File_path = "text.txt ";
// Determine whether a file exists
If (file_exists ($ file_path )){
// Determine whether the file can be opened
If ($ fp = fopen ($ file_path, "a + ")){
$ Buffer = 1024;
// Read and judge whether the object ends
$ Str = "";
While (! Feof ($ fp )){
$ Str. = fread ($ fp, $ buffer );
}
} Else {
Echo "file cannot be opened ";
}
} Else {
Echo "this file does not exist ";
}
// Replace the character
$ Str = str_replace ("\ r \ n ","
", $ Str );
Echo $ str;
Fclose ($ fp );
Functions used to read the INI configuration file:
$ Arr = parse_ini_file ("config. ini ");
// The returned result is an array.
Echo $ arr ['host']."
";
Echo $ arr ['username']."
";
Echo $ arr ['password']."
";