Three ways for PHP to read the contents of a file

Source: Internet
Author: User

  This article mainly introduces PHP to read the contents of the three ways, the need for friends can refer to the following

PHP three ways to read the contents of a file:   //************** The first way to read *****************************  code is as follows: Header ("Content-type : Text/html;charset=utf-8 "); //file path   $file _path=" text.txt "; //judge whether there is this file   if (file_exists ($file _path) {  if ($fp =fopen ($file _path, "A +")) { //Read file   $conn =fread ($fp, FileSize ($file _path)); // Replace string   $conn =str_replace ("RN", "<br/>", $conn);  echo $conn. " <br/> "; }else{  echo" file cannot be opened "; } }else{  echo" does not have this file; }  fclose ($FP);    //******************* The second way to read ***************************    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 ("RN", "<br/>", file_get_contents ($file _path));  echo $conn;  fclose ($fp);   //****************** The third way to read, loop read *****************    code as follows: Header ("Content-type:text/html;charset =utf-8 "); File path   $file _path= "text.txt"; //Determine if file exists   if (file_exists ($file _path)) { //Determine if file can open   if ( $FP =fopen ($file _path, "A +")) {  $buffer =1024; //edge reading to determine whether the end of the file is at   $STR = "";  while (!feof ($fp)) {   $STR. =fread ($fp, $buffer); } }else{  echo "file cannot open"; } }else{  echo "does not have this file"; nbsp } //Replacement characters   $STR =str_replace ("RN", "<br>", $str);  echo $str;  fclose ($fp);  Functions that read the INI configuration file:  $arr =parse_ini_file ("Config.ini"); //Returns the array   echo $arr [' Host ']. " <br/> ";  echo $arr [' username ']." <br/> ";  echo $arr [' Password ']." <br/> ";   

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.