Php can read file content in three ways (convert ). Php reads the file content: ********************** * ****** header (content-type: texthtml?charset=8w.w.w.w.w.w.file_pathtext.txt; php reads the file content:
- // ************** First method of reading ******************** *********
- 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 *************** ************
- 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 *****************
- 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']."
";
Reference link:
Php code for reading and processing the file content to strings
How to Read file content in php
******************* * ********* Header ("content-type: text/html; charset = utf-8 "); // file path $ file_path =" text.txt ";...