PHP——文件创建/写入

创建文件

  • fopen()
  • 如果你用fopen打开并不存在的文件,此函数会创建文件
  • 运行时发生错误,检查您是否有向硬盘写入信息的PHP 文件访问权限
<?php
$myfile =fopen("fopen.txt","w");
?>


写入文件

  • fwrite()
  • 第一个参数包含要写入文件的文件名
  • 第二个参数被写的字符串
<html>
<body>
<?php
$myfile = fopen("fopen.txt","w") or die("Unable to open file!");
$txt="Bill Gates
";
fwrite($myfile,$txt);
$txt="Steve Jobs
";
fwrite($myfile,$txt);
fclose($myfile);
?>
</body>
</html>


覆盖

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

补充说明:

  • die为函数输出一条消息,并退出当前脚本
  • 将一个文件关闭再次打开,再写入内容,便是覆盖,那么我要是在原有内容上追加该怎么做。
原文地址:https://www.cnblogs.com/ceiling-/p/14395195.html