php - 如何使用按钮打开我在 php 中创建的最后一个文件
问题描述
我能够使用用户在表单中输入的数据创建一个文件。第一个输入字段接受用户输入的内容并将其作为文件的标题。内容或消息取自 textarea 标签并将其添加到此文件中,当我单击提交时,文件被创建。问题是我有另一个按钮,当我单击时应该能够显示我在上一步创建的文件的内容。我正在使用另一个提交按钮来尝试将文件的内容显示到网页上。
这是用于创建文件的代码。
if(isset($sub) && $fname && $txt){
$myFile = fopen("$fname.txt", 'wb');
fwrite($myFile, $txt);
fclose($myFile);
解决方案
只需保存文件的路径,$_SESSION
如果您想在所有会话中保存它,您可以使用它。
session_start();
$path = "$fname.txt";
if(!$myFile = fopen($path, 'wb')) // Checks if the file can be opened
{
exit;
}
if (!fwrite($myFile, $txt)) // Checks if the write was successful
{
exit;
}
$_SESSION["recentFile"] = $path;
fclose($myFile);
现在您可以使用访问最后一条路径
session_start();
echo file_get_contents($_SESSION["recentFile"]);
推荐阅读
- python - 熊猫根据条件创建新列
- react-native - 反应原生信用卡输入
- javascript - Illustrator ScriptUI 嵌套对话框按钮未正常运行
- ios - 如何在 iOS 上设置以秒为单位的相机捕捉延迟?
- r - 在 R 中创建多个 Chisq/t 检验的更有效方法是什么?(使用泰坦尼克号数据)
- .net - 文件上传/下载 IOException:该进程无法访问文件'',因为它正在被另一个进程 .net 核心使用
- typescript - 如何从使用 export = 的模块重新导出 *
- javascript - JavaScript 函数:作为对象的结构化参数?
- android - Android:为什么长按会破坏自动背景?
- angular - 如何从 FormGroup 发布 NULL 值