Three methods for php to read file content:
// ************** First method of reading ******************** *********
Copy codeThe 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", "<br/>", $ conn );
Echo $ conn. "<br/> ";
} Else {
Echo "file cannot be opened ";
}
} Else {
Echo "this file does not exist ";
}
Fclose ($ fp );
// ******************* Second read method *************** ************
Copy codeThe 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", "<br/>", file_get_contents ($ file_path ));
Echo $ conn;
Fclose ($ fp );
// ******************** The third read method, cyclically read *****************
Copy codeThe 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", "<br>", $ 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']. "<br/> ";
Echo $ arr ['username']. "<br/> ";
Echo $ arr ['Password']. "<br/> ";