【问题标题】:PHP Creating and editing TXT filePHP 创建和编辑 TXT 文件
【发布时间】:2013-03-28 12:36:44
【问题描述】:

我已经尝试了所有方法,以及多个不同的源代码,我尝试过创建文件,所以 PHP 所要做的就是在其中写入,但仍然无法做到。

这是我在 index.html 上的 HTML

<form name="userinput" action="usersave.php" method="post">
Your name: <input type="text" name="username><br>
Your email: <input type="text" name="useremail"><br>
Your story: <textarea rows="3" cols="50" name="userstory"></textarea><br>
<input type="submit" value="Submit!" name="submit">
</form>

这是我在 usersave.php 上的 PHP

<header>
<?php
    $file = 'output.txt';
    $buffer = $_POST['userstory'];

    if (file_exists($file)) {
            $buffer = file_get_contents($file) . "\n" . $buffer;
    }

    $success = file_put_contents($file, $buffer);
?>
</header>

感谢您的帮助,谢谢。

【问题讨论】:

  • 任何错误或相关的日志文件信息?
  • 您是否遇到了一些权限错误?
  • 我没有使用任何主机,而是直接从我的 PC 将文件打开到 Chrome 中。不确定上述任何一项。
  • 你的意思是,Windows Explorer > YourFolder > thisfile.php > 打开文件?

标签: php file store


【解决方案1】:

编辑:您需要安装某种服务器环境才能开始使用。

我推荐WAMP

【讨论】:

  • 我没有使用任何主机,而是直接从我的 PC 将文件打开到 Chrome 中。不确定上述任何一项。
  • 我假设您使用的 windows (chown -R www-data:www-data /dir/to/www/ 不适用于 windows) 等一下.. 给你一个关于如何阅读的示例代码/ 使用 php 写入 / 到文件
  • 编辑了我的 windows 答案
【解决方案2】:

如果你不使用任何主机,那么就没有服务器来运行 php,浏览器不解析 PHP,服务器解析,所以如果没有服务器,则没有 PHP被解析。

【讨论】:

  • 非常感谢,没想到这么简单。
  • 等等,你只是想用chrome打开php文件?
  • 现在看我的答案,你应该检查 WAMP
【解决方案3】:
<?php
$file_path = "text_file.txt";
$file_handle = fopen($file_path, 'r'); // OPEN FILE READY FOR 'R'eading!
$text_data = fread($file_handle, filesize($file_path));  //Read actual data in file
print $text_data; // print the data you can use echo if you like
fclose($file_handle); // Close the file read / buffer


$data_to_write = "This is the data that will be written to disk!";
$file_path = "new_file.txt";
$file_handle = fopen($file_path, 'w');  // OPEN FILE READY FOR 'W'riting!
fwrite($file_handle, $data_to_write);
fclose($file_handle);
?>

【讨论】:

  • 尽管这个答案很好,但我相信我们只是确定他实际上并没有运行 apache 或其他任何东西,只是试图在 chrome 中打开文件......
  • 是的,在我创建示例代码之后意识到这一点。他没有安装任何 apache / php ......但是,猜想他会带着这个问题回来,因为他的代码不会做他想做的事
【解决方案4】:

我没有使用任何主机,而是直接从我的 PC 将文件打开到 Chrome 中。

您无法在没有服务器(例如 Apache)的情况下运行 PHP 文件。 PHP 文件是“服务器端”脚本;脚本在服务器上执行,结果输出由服务器发送到浏览器。

看到这个问题:

Parsing PHP content in HTML without Webserver

或者尝试用谷歌搜索

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多