This article mainly introduces three methods of reading file content in PHP. Friends in need can refer to it
Three ways to read file content in php:
//**************The first reading method****************************** *
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 "File cannot be opened";
}
}else{
echo "There is no such file";
}
fclose($fp);
//************************Second reading 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("rn","
",file_get_contents($file_path));
echo $conn;
fclose($fp);
//******************The third reading method, loop reading****************
The code 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 be opened
if($fp=fopen($file_path,"a+")){
$buffer=1024;
//Determine 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 "There is no such file";
}
//Replace characters
$str=str_replace("rn","
",$str);
echo $str;
fclose($fp);
Function to read INI configuration file:
$arr=parse_ini_file("config.ini");
//returns an array
echo $arr['host']."
";
echo $arr['username']."
";
echo $arr['password']."
";
http://www.bkjia.com/PHPjc/730210.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/730210.htmlTechArticleThis article mainly introduces three methods of reading file content in php. Friends who need it can refer to php reading. Three ways to get the file content://****************The first reading method******************...
Statement: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