Three ways to read PHP files

Source: Internet
Author: User
Tags fread
PHP Three ways to read the contents of a file:

First read mode *****************************
Header ("Content-type:text/html;charset=utf-8");//file path $file_path= "Text.txt";//Determine if 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 ("\ r \ n "," <br/> ", $conn); Echo $conn." <br/> ";} Else{echo "File not open";}} Else{echo "no this file";} Fclose ($FP);
Second Read mode **********************************
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", "<br/>", file_get_contents ($file _path)); Echo $conn; fclose ($FP);
Third read mode, loop read ******************************
header ("Content-type:text/html;charset=utf-8");//file path $file_path= "Text.txt";//determine if the file exists if (File_ Exists ($file _path)) {//Determine if the file can open if ($fp =fopen ($file _path, "A +") {$buffer =1024;//side read to determine if the end of the file $str= ""; while (!feof ( $FP) {$str. =fread ($fp, $buffer);}} Else{echo "file cannot be opened";}} Else{echo "no this file";} Replace character $str=str_replace ("\ r \ n", "<br>", $str), Echo $str, fclose ($fp), function to read the INI configuration file: $arr =parse_ini_file (" Config.ini ");//Returns an array of echo $arr [' Host ']. <br/> "; Echo $arr [' username ']." <br/> "; Echo $arr [' Password ']." <br/> "; 
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.