php - 如何使用 PHP 创建并保存到文本文件?
问题描述
我一直在做一个测验,我发现需要将我的答案保存到一个文本文件中。我想出了一些代码:
<?php
$myfile = fopen("myfile.txt", "r+") or die("Unable to open file!");
$txt = "Name:\n";
fwrite($myfile, $txt);
$txt = "Student1\n";
fwrite($myfile, $txt);
$txt = "Marks:\n";
fwrite($myfile, $txt);
$txt = "0\n";
fwrite($myfile, $txt);
echo fread($myfile,filesize("myfile.txt"));
fclose($myfile);
?>
但我注意到这只是想写入一个内容已经存在的文件,并且它无法创建一个新文件并写入它。有什么办法可以吗?
解决方案
它应该w+
代替r+
$myfile = fopen("myfile.txt", "w+")
推荐阅读
- android - 解析失败:Landroid/arch/core/executor/AppToolkitTaskExecutor
- primefaces - JSF ViewScoped bean 未实例化
- c# - 如何将 Unity Display 对话框的输出更改为特定的 Gameobject?
- wpf - Interaction.Triggers 数据模板列 WPF
- linux - 打开终端,运行命令,稍后返回 SAME 终端并执行另一个命令
- java - 未找到处理 Intent 的 Activity { act=android.intent.action.OPEN_DOCUMENT cat=[android.intent.category.OPENABLE] typ=/* }
- html - 我的 CSS 没有改变,为什么?
- python - 查找两个列表中的共同元素
- select - 编译错误:UniData LSELECT
- android - Firebase 搜索不适用于 FirebaseRecylcerOptions