【问题标题】:How do I create and save to a text file using PHP?如何使用 PHP 创建并保存到文本文件?
【发布时间】:2020-08-07 04:38:10
【问题描述】:

我一直在做一个测验,我发现需要将我的答案保存到一个文本文件中。我想出了一些代码:

<?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);
?>

但我注意到这只是想写入一个内容已经存在的文件,并且它无法创建一个新文件并写入它。有没有办法做到这一点?

【问题讨论】:

    标签: php text


    【解决方案1】:

    应该是w+ 而不是r+

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多