An example of three possible ways for php to read file contents is presented
- 2020-12-26 05:36:47
- OfStack
3 ways php can read file contents:
/ / * * * * * * * * * * * * * * 1 kind of way to read * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/ / * * * * * * * * * * * * * * * * * * * 2 kinds of reading way * * * * * * * * * * * * * * * * * * * * * * * * * * *
//***************** the third reading mode, the loop reads ****************** ***
/ / * * * * * * * * * * * * * * 1 kind of way to read * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
header("content-type:text/html;charset=utf-8");
// The file path
$file_path="text.txt";
// Determine if the file exists
if(file_exists($file_path)){
if($fp=fopen($file_path,"a+")){
// Read the file
$conn=fread($fp,filesize($file_path));
// Substitution string
$conn=str_replace("\r\n","<br/>",$conn);
echo $conn."<br/>";
}else{
echo " The file won't open ";
}
}else{
echo " I don't have this file ";
}
fclose($fp);
/ / * * * * * * * * * * * * * * * * * * * 2 kinds of reading way * * * * * * * * * * * * * * * * * * * * * * * * * * *
header("content-type:text/html;charset=utf-8");
// The 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 reading mode, the loop reads ****************** ***
header("content-type:text/html;charset=utf-8");
// The file path
$file_path="text.txt";
// Determine if the file exists
if(file_exists($file_path)){
// Determine if the file can be opened
if($fp=fopen($file_path,"a+")){
$buffer=1024;
// Read to see if you've reached the end of the file
$str="";
while(!feof($fp)){
$str.=fread($fp,$buffer);
}
}else{
echo " File cannot be opened ";
}
}else{
echo " I don't have this file ";
}
// Substitution characters
$str=str_replace("\r\n","<br>",$str);
echo $str;
fclose($fp);
read INI Config file functions :
$arr=parse_ini_file("config.ini");
// It returns an array
echo $arr['host']."<br/>";
echo $arr['username']."<br/>";
echo $arr['password']."<br/>";