PHP 文件创建/写入

时间:2023-03-08 16:42:02
<?php 

/*
PHP 文件创建/写入
fopen() 函数也用于创建文件。也许有点混乱,
但是在 PHP 中,创建文件所用的函数与打开文件的相同。
如果您用 fopen() 打开并不存在的文件,此函数会创建文件,
假定文件被打开为写入(w)或增加(a)。 PHP 写入文件 - fwrite()
fwrite() 函数用于写入文件。
fwrite() 的第一个参数包含要写入的文件的文件名,
第二个参数是被写的字符串。 PHP 覆盖(Overwriting)
如果现在 "newfile.txt" 包含了一些数据,
我们可以展示在写入已有文件时发生的的事情。
所有已存在的数据会被擦除并以一个新文件开始。
*/
header("Content-type: text/html; charset=utf-8");
$myfile=fopen("testfile.txt","w")or die("unable to open file!");
$txt="Bill Gates \n";
fwrite($myfile,$txt);
$txt="server jobs \n";
fwrite($myfile,$txt);
fclose($myfile); $myfile1=fopen("testfile.txt","r");
while(!feof($myfile1)){
echo fgets($myfile1)."<br> ";
} $myfile2=fopen("testfile.txt","w")or die("unable to open file!");
$txt="Mickey Mouse\n";
fwrite($myfile2,$txt);
$txt="Minnie Mouse\n";
fwrite($myfile2,$txt);
fclose($myfile2);
$myfile3=fopen("testfile.txt","r");
while(!feof($myfile3)){
echo fgets($myfile3)."<br> ";
}
?>