在Web开辟中,我们经常必要读取和处置惩罚文本文件。PHP作为一种盛行的服务器端脚本语言,提供了多种方法来读取TXT文本内容。本文将先容五种不同的PHP教程,帮助您学习怎样利用PHP读取TXT文本内容。PHP读取文件内容在实际开辟当中,还是比力常见的,所以今天我就给大家分享几种读取的方法,大家可以选择一种最适合的就行了。 第一种,利用fread函数: [code]<?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); fclose($fp); } ?>[/code]第二种,用file_get_contents函数: [code]<?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; } ?>[/code]第三种,用fopen函数: [code]<?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; fclose($fp); } ?>[/code]第四种方法,利用file函数: [code]<?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 />"; fclose($file_arr); } } ?>[/code]第五种,还是利用fopen函数: [code]<?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; fclose($fp); } ?> [/code]固然,开启资源后,记得利用fclose($fp);关闭一下,不然的话,会斲丧服务器的资源。 方法增补 除了上文的方法,小编还为大家整理了其他一些PHP读取TXT文本的方法,希望对大家有所帮助 php读取文件内容的三种方法: [code]//**************第一种读取方式***************************** 代码如下: header("content-type:text/html;charset=utf-8"); //告诉php预处置惩罚器将内容已utf8的格式传递给浏览器 //文件路径 $file_path="text.txt"; //判断是否有这个文件 if(file_exists($file_path)){ if(fp=fopen(file_path,"a+")){ //读取文件 conn=fread(fp,filesize($file_path)); //替换字符串 conn=strreplace("rn","<br/>",conn); echo $conn."<br/>"; }else{ echo "文件打不开"; } }else{ echo "没有这个文件"; } fclose($fp); //*******************第二种读取方式*************************** 代码如下: header("content-type:text/html;charset=utf-8"); //文件路径 $file_path="text.txt"; conn=filegetcontents(file_path); conn=strreplace("rn","<br/>",filegetcontents(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=strreplace("rn","<br>",str); echo $str; fclose($fp); [/code]利用fopen,file,file_get_contents函数来实现读取文本文件内容 [code]//fopen 读取文件实例,代码如下: $path ='a.txt'; $fp=fopen($file,"r");//以只读的方式打开文件 while(!(feof($fp))) { $text=fgets($fp);//读取文件的一行 echo $text; } //file_get_contents读取文件,代码如下: if( file_exists( $path ) ) { $body = file_get_contents($path); echo $body ;//输入文件内容 } else { echo "文件不存在 $path"; }//开源代码phpfensi.com //读取文本文件,代码如下: $cbody = file($path); print_r($cbody); //由于file读取出来的文件是以数组形式生存的,所以用print_r输出。[/code]到此这篇关于PHP读取TXT文本内容的五种实用方法小结的文章就先容到这了,更多相关PHP读取TXT内容请搜索脚本之家从前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 来源:https://www.jb51.net/program/3132339ou.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 21:47 , Processed in 0.030311 second(s), 18 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.