Home > Backend Development > PHP Tutorial > Examples of three possible methods for reading file content in PHP_PHP Tutorial

Examples of three possible methods for reading file content in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:39:32
Original
756 people have browsed it

Three methods for php to read file content:

//**************The first reading method************ *******************

Copy code 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("rn","
",$conn);
echo $conn."
}else{
echo "The file cannot be opened";
}
}else{
echo "There is no such file";
}
fclose($ fp);

//************************Second reading method**************** ****************
Copy code 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("rn","
",file_get_contents($file_path));
echo $conn;
fclose($fp);

//******************The third reading method, loop reading**********************
Copy code The code is as follows:

header("content-type:text/html;charset=utf-8");
//File path
$file_path="text.txt";
//Judge whether the file exists
if(file_exists($file_path)){
//Judge whether the file can be opened
if($fp=fopen($file_path,"a+")){
$buffer=1024;
//Judge whether the end of the file is reached while reading
$str="";
while(!feof($fp)){
$str.=fread($fp,$buffer);
}
}else{
echo "File cannot be opened";
}
}else{
echo "No such file";
}
//Replacement characters
$str=str_replace("rn","
",$str) ;
echo $str;
fclose($fp);
Function to read INI configuration file:
$arr=parse_ini_file("config.ini");
//returned is an array
echo $arr['host']."
";
echo $arr['username']."
";
echo $arr ['password']."
";

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/730057.htmlTechArticleThree methods for php to read file content: //************ **The first reading method**************************** Copy the code as follows: header("content-type:text /html;charset=utf-8"); /...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template