第一种方法-----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);
fclose($fp);
}在这里插入代码片
?>
第二种方法-----file_get_contents()
<?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;
while(!feof($fp)){
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
fclose($fp);
}
?>
第四种方法
<?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);
}
}
?>
第五种方法
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
fclose($fp);
}
?>