php写⼊、追加写⼊⽂件的实例1$myfile = fopen("", "w") or die("Unable to open file!");
2$txt = "Bill Gates\n";
3fwrite($myfile, $txt);
4$txt = "Steve Jobs\n";
5fwrite($myfile, $txt);
6//记得关闭流
7fclose($myfile);
fopen() 函数也⽤于创建⽂件。也许有点混乱,但是在 PHP 中,创建⽂件所⽤的函数与打开⽂件的相同。如果您⽤ fopen() 打开并不存在的⽂件,此函数会创建⽂件,假定⽂件被打开为写⼊(w)或增加(a)。判断⽂件是否存在,不存在就创建,存在就追加
fopen 创建文件1if(file_exists(''))
2 {
3//"当前⽬录中,⽂件存在",追加
4$myfile = fopen("", "a") or die("Unable to open file!");
5$txt = "\n【".date('Y-m-d H:i:s',time())."】---"."成功回调";
6fwrite($myfile, $txt);
7//记得关闭流
8fclose($myfile);
9 }
10else
11 {
12//"当前⽬录中,⽂件不存在",新写⼊
13$myfile = fopen("", "w") or die("Unable to open file!");
14$txt = "【".date('Y-m-d H:i:s',time())."】---"."成功回调";
15fwrite($myfile, $txt);
16//记得关闭流
17fclose($myfile);
18 }
或者⽤a+模式,⽐如这样:
1$myfile = fopen("notify_error.log", "a+") or die("Unable to open file!");
2$txt = "【".date('Y-m-d H:i:s',time())."】---".$rec"\r\n";
3fwrite($myfile, $txt);
4//记得关闭流
5fclose($myfile);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论