gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

详解php读取文件的三种方法

来源:http://erdangjiade.com/topic/1162.html 你好,世界。 2017-09-25 22:42浏览(26)

php读取文件内容的三种方法:

//**************第一种读取方式*****************************
header("content-type:text/html;charset=utf-8");
//文件路径
$file_path="text.txt";
//判断是否有这个文件
if(file_exists($file_path)){
if($fp=fopen($file_path,"a+")){
//读取文件
$conn=fread($fp,filesize($file_path));
//替换字符串
$conn=str_replace("
","<br/>",$conn);
echo $conn."<br/>";
}else{
echo "文件打不开";
}
}else{
echo "没有这个文件";
}
fclose($fp);
//*******************第二种读取方式**********************************
header("content-type:text/html;charset=utf-8");
//文件路径
$file_path="text.txt";
$conn=file_get_contents($file_path);
$conn=str_replace("
","<br/>",file_get_contents($file_path));
echo $conn;
fclose($fp);
//******************第三种读取方式,循环读取******************************
header("content-type:text/html;charset=utf-8");
//文件路径
$file_path="text.txt";
//判断文件是否存在
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("
","<br>",$str);
echo $str;
fclose($fp);
读取INI配置文件的函数:
$arr=parse_ini_file("config.ini");
//返回的是数组
echo $arr['host']."<br/>";
echo $arr['username']."<br/>";
echo $arr['password']."<br/>";

以上就是详解php读取文件的三种方法的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2