Five ways for PHP to read file content: _ PHP Tutorial

Source: Internet
Author: User
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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.