本篇文章主要介绍php五种读取文件内容方法,感兴趣的朋友参考下,希望对大家有所帮助。
-----第一种方法-----fread()--------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
?>--------第二种方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>-----第三种方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>-------第四种方法--------------
立即学习“PHP免费学习笔记(深入)”;
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>----第五种方法--------------------
功能介绍:1、模块化的程序设计,使得前台页面设计与程序设计几乎完全分离。在前台页面采用过程调用方法。在修改页面设计时只需要在相应位置调用设计好的过程就可以了。另外,这些过程还提供了不同的调用参数,以实现不同的效果;2、阅读等级功能,可以加密产品,进行收费管理;3、可以完全可视化编辑文章内容,所见即所得;4、无组件上传文件,服务器无需安装任何上传组件,无需支持FSO,即可上传文件。可限制文件上传的类
0
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
以上就是PHP五种读取文件内容方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号