推荐使用file_get_content函数读取,再一次性echo
<php$file = 'welcometxt';
$content = '';
if(is_file($file)){
$content = file_get_content($file);// 一次性取完
}
//echo $content; //这个是直接echo,一般文字会挤在一起,推荐用下面的
echo '<pre>'htmlspecialchars($content) '</pre>'; // 这个是原格式
asp和php都可以
asp的
Dim fso,gf,str
Set fso = CreateObject("ScriptingFileSystemObject")
Set gf = fsoOpenTextFile("c:\xxxtxt")
str = gf ReadLine()
ResponseWrite str
gfClose()
php的
<php
header("Content-Type:text/html;charset:utf8");
$fp=fopen("usertxt","rb");//usertxt你的TXT路径
>
<php
//phpinfo();
$a_content = file_get_contents('atxt');
$str = strtok($a_content,"\r");
echo $str;
echo strlen($str);
for($i = 0; $i < strlen($str); $i++)
{
if(is_numeric($str[$i]))
{
echo $str[$i]"<br/>";
}
}
>
第一种读取方式<br>
<
$file_path ="testtxt";
if(file_exists($file_path)){ //先判断文件是否存在
//打开文件
$fp = fopen($file_path,"a+");
//读取文件内容
$con = fread($fp,filesize($file_path));
echo "文件的内容是:<br>"$con;
//在默认情况下,得到的内容输出到网页后,不会换行,因为网页不认\r\n是换行符,把\r\n体换成<br />
$con = str_replace("\r\n","<br />",$con);
echo "<br>文件的内容是:<br>"$con;
//关闭
fclose($fp);
}else{
echo "文件不存在!";
}
>
<hr>
第二种读取方式<br>
<php
if(file_exists($file_path)){
$con = file_get_contents($file_path);
$con = str_replace("\r\n","<br />",$con);
echo "文件的内容是:<br>"$con;
}else{
echo "文件不存在!";
}
>
<hr>
第三种读取方式(大文件、循环读取)<br>
<php
$fp = fopen($file_path,"a+");
$buffer = 1024; //设置读取1024个字节
$str = "";
//一边读,一边判断是否到达文件末尾
while(!feof($fp)){
$str= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
fclose($fp);
>
$file = file("atxt");
foreach($text_array as $value){
$text_array[]=explode('<--->',$value);
}
print_r($text_array);
<php
$fp = fopen('welcometxt', 'r');
if (!$fp) {
echo 'Could not open file somefiletxt';
}
while ($char = fgetc($fp)) {
echo "$char";
}
>
php读取文件内容:
-----第一种方法-----fread()--------
<php
$file_path = "testtxt";
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 = "testtxt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
>
-----第三种方法------------
<php
$file_path = "testtxt";
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
$file_path = "testtxt";
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 />";
}/
}
>
----第五种方法--------------------
<php
$file_path = "testtxt";
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 读取txt 显示全部的内容,包括:php 读取txt 显示、PHP或ASP能读取TXT文件内的信息吗、php 获取txt文本第一行内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)