php應該如何讀取文件內容呢,下面小編爲大家介紹三種php讀取文件內容的方法,歡迎大家閱讀!
php讀取文件內容的'方法第一種讀取方式
代碼如下:
header("content-type:text/html;charset=utf-8");
//文件路徑
$file_path="";
//判斷是否有這個文件
if(file_exists($file_path)){
if($fp=fopen($file_path,"a+")){
//讀取文件
$conn=fread($fp,filesize($file_path));
//替換字符串
$conn=str_replace("rn","
",$conn);
echo $conn."
";
}else{
echo "文件打不開";
}
}else{
echo "沒有這個文件";
}
fclose($fp);
第二種讀取方式
代碼如下:
header("content-type:text/html;charset=utf-8");
//文件路徑
$file_path="";
$conn=file_get_contents($file_path);
$conn=str_replace("rn","
",file_get_contents($file_path));
echo $conn;
fclose($fp);
第三種讀取方式,循環讀取
代碼如下:
header("content-type:text/html;charset=utf-8");
//文件路徑
$file_path="";
//判斷文件是否存在
if(file_exists($file_path)){
//判斷文件是否能打開
if($fp=fopen($file_path,"a+")){
$buffer=1024;
//邊讀邊判斷是否到了文件末尾
$str="";
while(!feof($fp)){
$str.=fread($fp,$buffer);
}
}else{
echo "文件不能打開";
}
}else{
echo "沒有這個文件";
}
//替換字符
$str=str_replace("rn","
",$str);
echo $str;
fclose($fp);
讀取INI配置文件的函數:
$arr=parse_ini_file("");
//返回的是數組
echo $arr['host']."
";
echo $arr['username']."
";
echo $arr['password']."
";