PHP provides five methods to read file content ,. Five ways for PHP to read file content: five ways for php to read file content to share the five methods for php to read file content: Well, after writing it, it is found that all files are not closed. There are five methods for PHP to read file content,
Five methods for php to read file content
Share the following five methods for php to read file content: Well, after writing, I found that all the files are not closed. In actual application, close fclose ($ fp );
--
Php reads the file content:
----- Method 1 ----- fread ()--------
<? Php $ file_path = "test.txt"; if (file_exists ($ file_path) {$ fp = fopen ($ file_path, "r"); $ str = fread ($ fp, filesize ($ file_path); // specify the read size. here, the entire file is read. echo $ str = str_replace ("\ r \ n ","
", $ Str) ;}?>
-------- Method 2 ------------
<? Php $ file_path = "test.txt"; if (file_exists ($ file_path) {$ str = file_get_contents ($ file_path ); // read the entire file content into a string $ str = str_replace ("\ r \ n ","
", $ Str); echo $ str ;}?>
----- Method 3 ------------
<? Php $ file_path = "test.txt"; if (file_exists ($ file_path) {$ fp = fopen ($ file_path, "r"); $ str = ""; $ buffer = 1024; // read 1024 bytes each time while (! Feof ($ fp) {// cyclically read until the entire file is read $ str. = fread ($ fp, $ buffer);} $ str = str_replace ("\ r \ n ","
", $ Str); echo $ str ;}?>
------- Method 4 --------------
<? Php $ file_path = "test.txt"; if (file_exists ($ file_path) {$ file_arr = file ($ file_path); for ($ I = 0; $ I
";}/* Foreach ($ file_arr as $ value) {echo $ value ."
";} */}?>
---- Method 5 --------------------
<? Php $ file_path = "test.txt"; if (file_exists ($ file_path) {$ fp = fopen ($ file_path, "r"); $ str = ""; while (! Feof ($ fp) {$ str. = fgets ($ fp); // read data row by row. If fgets does not write the length parameter, the default value is 1 k .} $ Str = str_replace ("\ r \ n ","
", $ Str); echo $ str ;}?>
The above content is shared with you five ways PHP can read file content.
Articles you may be interested in:
- PHP code for reading and modifying a line of content in a large file
- PHP reads the file content code (txt, js, etc)
- PHP implementation code for reading webpage files (fopen, curl, etc)
- PHP reads the content of the txt file and assigns it to the array code
- Php reads the content of the file to a string, while removing line breaks, blank lines, and spaces at the beginning and end of the line (original Zjmainstay)
- Detailed description of csv file reading based on PHP
- PHP code for reading the content from rows X to lines Y of large files
- Several methods for php to read file content
- PHP clears the sample code after reading the file content
- Summary of methods for php to read file content
- Php method for reading the file content to an array
Tips: five ways for php to read file content to share the five methods for php to read file content: Well, after writing, I found that all the files are not closed. Actual...