php 文件操作

<!DOCTYPE html>
<body>
<html>

<?php
//echo readfile("file.txt");
$fp=fopen("file.txt","x") or die("Unable to open file!");//fopen用于打开和创建文件

// echo fread($fp,filesize("file.txt"));
/* while(!feof($fp)) //f end of file
{
//echo fgetc($fp)."<br>"; 读取单个字符
echo fgets($fp)."<br>"; //读取行
}*/


$txt="有你";
fwrite($fp,$txt);//r+写入的文件会覆盖原文件的内容,其余的内容不变,写完文件的指针指向了覆盖后的地方
//w w+写入的内容会完全覆盖原文件的内容,写完文件指针指向了文件的尾部
//a a+在原有的文件上的尾部写,写完文件指针指向了尾部
// x 若文件已经存在,返回错误,不存在就创建文件,返回正确
while(!feof($fp)) //f end of file
{
echo fgets($fp)."<br>";
}
fclose($fp);
?>
</body>
</html>

原文地址:https://www.cnblogs.com/jiangger/p/6439809.html