PHP 文件创建/写入


 

下面的例子创建名为 "testfile.txt" 的新文件。此文件将被创建于 PHP 代码所在的相同目录中:

$myfile = fopen("testfile.txt", "w")

 

下面的例子把姓名写入名为 "newfile.txt" 的新文件中:

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

打开 "newfile.txt" 文件,它应该是这样的:

Bill Gates
Steve Jobs

 

 

如果现在 "newfile.txt" 包含了一些数据,所有已存在的数据会被擦除并以一个新文件开始。

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

如果现在我们打开这个 "newfile.txt" 文件,Bill 和 Steve 都已消失,只剩下我们刚写入的数据:

Mickey Mouse
Minnie Mouse

 

参考:

https://www.w3school.com.cn/php/php_file_create.asp

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM