【问题标题】:Why is post method not allowed when i write to a file in php?为什么我在 php 中写入文件时不允许使用 post 方法?
【发布时间】:2022-09-30 18:07:18
【问题描述】:

所以我是 php 的新手,我正在完成一项学校任务。它是一种简单的形式,它将接受输入并将其写入 txt 文件。但对我来说它不起作用。我试图制作一个测试版本来查看问题出在哪里,我得到的错误是 405 方法不允许。我也使用过 fopen 和 fwrite,但它们也不起作用。

这是我的html代码

<!DOCTYPE html>
<html lang=\"No\">
<head>
    <meta charset=\"UTF-8\">
    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
    <title>Index1</title>
</head>
<body>
 
  <form method=\"post\" action=\"test21.php\" name=\"myForm\">
    klassekode <input type=\"text\" id=\"klassekode\" name=\"klassekode\" required />
    <input type=\"submit\" value=\"Fortsett\" id=\"fortsett\" name=\"fortsett\" />
  </form>
 <a href=\"klasse.txt\">Vis  klasser </a> 
               
</body> 
</html>


以及我用来测试 test21.php 的非常简化的 php 代码

<?php
file_put_contents(\"klasse.txt\", \"test\");
?>
  • \"405 method not allowed\" 表示您不允许向该 URL 发出 POST 请求。 PHP 没有运行,所以问题与您所询问的无关。
  • 在这种情况下 405 Method Not Allowed 通常意味着 Web 服务器认为目标文件是“静态的”,因此不应接受 POST 请求。检查您的 Web 服务器是否已正确设置以在以 .php 开头的文件中执行 PHP。
  • 您尝试过什么来解决问题?你被困在哪里了?您确定这与您的 PHP 文件中的代码有任何关系吗?

标签: php html forms file-handling


【解决方案1】:

“405 方法不允许”意味着您不允许向该 URL 发出 POST 请求。

您的屏幕截图显示您正在向在端口 5500 上运行的服务器发出请求。

这是 VS Code Live Server 的默认端口,所以我假设您正在使用它。

VS Code Live Server 不支持 PHP 或任何形式的服务器端编程,并且会为 POST 请求提供 405 错误。

您需要切换到支持 PHP 的 HTTP 服务器。

【讨论】:

    【解决方案2】:

    您应该对 Web 服务器进行错误配置。它回复405,这意味着服务器不允许http方法POST。您可以看到 POST 也不在 Allow: 标头中。

    【讨论】:

      猜你喜欢
      • 2014-03-01
      • 2017-09-02
      • 1970-01-01
      • 2014-06-13
      • 2022-01-19
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多