Php three methods to read file content _ PHP Tutorial

Source: Internet
Author: User
Php can read file content in three ways. 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: * This article describes three methods for php to read files. method, for more information, see

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 this file exists if (file_exists ($ file_path) {if ($ fp = fopen ($ file_path, "a + ")) {// read file $ conn = fread ($ fp, filesize ($ file_path); // replace string $ conn = str_replace ("\ r \ n ","
", $ Conn); echo $ conn ."
";}Else {echo" file cannot be opened ";}} else {echo" This file is not available ";}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, the code for cyclically reading **************** 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 )) {// Determine whether the file can open if ($ fp = fopen ($ file_path, "a +") {$ buffer = 1024; // read and judge whether the file ends at $ str = ""; while (! Feof ($ fp) {$ str. = fread ($ fp, $ buffer) ;}} the else {echo "file cannot be opened" ;}} else {echo "This file is not available ";} // replace the character $ str = str_replace ("\ r \ n ","
", $ Str); echo $ str; fclose ($ fp); function used to read the INI configuration file: $ arr = parse_ini_file (" config. ini "); // The Returned array echo $ arr ['host']."
"; Echo $ arr ['username']."
"; Echo $ arr ['password']."
";

Javasphp can read the file content in three ways: // ************** first method of reading ***********...

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.