PHP three possible ways to read the contents of a file introduction to _php Tips

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

The first way of reading *****************************
Copy Code code as follows:

Header ("Content-type:text/html;charset=utf-8");
File path
$file _path= "Text.txt";
To determine if there is this file
if (file_exists ($file _path)) {
if ($fp =fopen ($file _path, "A +")) {
Reading files
$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 "does not have this document";
}
Fclose ($FP);

The second way of reading ***************************
Copy Code code 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", "<br/>", file_get_contents ($file _path));
echo $conn;
Fclose ($FP);

The third way of reading, cyclic reading *****************
Copy Code code as follows:

Header ("Content-type:text/html;charset=utf-8");
//file path
$file _path= "Text.txt";
//To determine if the file exists
if (file_exists ($file _path)) {
///To determine if the file can be opened
if ($fp =fopen ($file _path, "A +")) {
$buffer =10 24;
//Edge read to determine whether the end of the file
$str = "";
while (!feof ($fp)) {
$str. =fread ($fp, $buffer);
}
}else{
echo "file cannot be opened";
}
}else{
echo "does not have this file";
}
//replacement character
$str =str_replace ("\ r \ n", "<br>", $str);
Echo $str;
Fclose ($FP);
functions to 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.