PHP文件编程

来源:互联网 发布:淘宝卖的舒尔话筒假的 编辑:程序博客网 时间:2024/05/16 00:55
第一种方法:
<?php
//打开文件
$file_path="test.txt";
//判断文件是否存在
if(file_exists($file_path)){
//该函数返回一个指向文件的指针
$fp=fopen($file_path,"a+");
//读取内容,并输入
$con=fread($fp,filesize($file_path));
echo "文件的内容是:</br>";
//在默认情况下,我们得到的内容输出到网页后,不会换行,因为网页不认\r\n是换行符
//将\r\n替换成</br>
$con=str_replace("\r\n","</br>",$con);
echo $con;
fclose($fp);
}else{
echo "文件不存在";
}
?>

第二种方法:
<?php
$file_path="test.txt";
$con=file_get_contents($file_path);
$con=str_replace("\r\n","</br>",$con);
echo $con;
?>

第三种方法:
<?php
//循环读取方法,适用于大文件
$file_path="test.txt";
if(file_exists($file_path)){
$fp=fopen($file_path,"a+");
//设置一次读取1024个字节
$buffer=1024;
$str="";
//一边读取,一边判断是否到达文件末尾
while(!feof($fp)){
$str=fread($fp,$buffer);
}
$str=str_replace("\r\n","</br>",$str);
echo $str;
fclose($fp);
}else{
echo "文件不存在";
}
?>
0 0
原创粉丝点击