【问题标题】:File not being created in PHP?文件不是在 PHP 中创建的?
【发布时间】:2020-05-15 20:25:28
【问题描述】:

我用 PHP 编写了这段代码,因为我想练习文件处理。

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <form action = "index.php" method = "get">
    Do you want to make a new file (NF), edit a file (EF), or delete a file (DF)?
    <input type = "text" name = "FileHandling">
    <input type = "submit">
    <br/>
    </form>
    <?php
      $FileCommand = $_GET['FileHandling'];
      if($FileCommand == 'NF') {
        echo "<form action = 'index.php' method = 'get'><br/>";
        echo "What is the new file's name?<br/>";
        echo "<input type = 'text' name = 'CreateFile' method = 'get'><br/>";
        echo "<input type = 'submit'><br/>";
        $FileName = $_GET['CreateFile'];
        echo $FileName;
        if(null !== $FileName) {
          echo $FileName;
          echo "yes";
          $CreateTheFile = fopen($FileName, 'w');
        } else {
          echo "No file name chosen. ";
        }
      }
    ?>
  </body>
</html>

但是,选择“NF”并输入文件名后出现问题。文件未创建:

echo $FileName;
if(null !== $FileName) {
  echo $FileName;
  echo "yes";
  $CreateTheFile = fopen($FileName, 'w');
}

【问题讨论】:

  • 你的错误是 !== 它是 != 而不是。检查w3schools.com/php/php_operators.asp
  • 您是否启用了错误报告? According to the docs and this other SO question 失败时会有一个 E_WARNING 应该有失败的原因。
  • 你没有关闭你的第二个表格。
  • &lt;input type = 'text' name = 'CreateFile' method = 'get'&gt; - 只有表单有方法,没有输入。
  • 启用错误报告;你有太多语法错误。

标签: php forms file-handling


【解决方案1】:

您处理输入和表单的方式存在错误。目前,您正在检查$FileCommand == 'NF',这在第一次提交表单时是正确的。但随后页面重新加载,您将获得带有新输入的第二个表单。因此,当您填写第二个表单并重新提交时,现在 &lt;input name='FileHandling' /&gt; 未提交,因为它不是此表单的一部分(它是第一个表单的一部分)。

因此,如果您将 PHP 更改为以下内容,则将尝试在 $FileName !== null(无论 $FileCommand 的值如何)而不是您之前的逻辑(也需要 $FileCommand == 'NF')创建文件。这会将创建文件的逻辑移到第一个 if 之外。

<?php
    $FileCommand = $_GET['FileHandling'];
    if ($FileCommand == 'NF') {
        echo "<form action='index.php' method='get'><br/>";
        echo "What is the new file's name?<br/>";
        echo "<input type='text' name = 'CreateFile'><br/>";
        echo "<input type='submit'><br/>";
        echo "</form>";
    }

    $FileName = $_GET['CreateFile'];

    if (null !== $FileName) {
      echo $FileName;
      echo "yes";
      $CreateTheFile = fopen($FileName, 'w');
    } else {
      echo "No file name chosen. ";
    }
?>

另一种处理方法是只创建一个单独的字段,而不是一个完全独立的表单。

<html>
  <body>
    <form action="index.php" method="get">
        Do you want to make a new file (NF), edit a file (EF), or delete a file (DF)?
        <br>
        <input type="text" name="FileHandling" />
        <br>
        <?php
            $FileCommand = @$_GET['FileHandling'];
            if($FileCommand == 'NF') {
                echo "What is the new file's name?<br/>";
                echo "<input type='text' name = 'CreateFile' /><br/>";
            }
        ?>
        <input type="submit">
    </form>
    <?php
        $FileName = $_GET['CreateFile'];

        if(null !== $FileName) {
          echo $FileName;
          echo "yes";
          $CreateTheFile = fopen($FileName, 'w');
        } else {
          echo "No file name chosen. ";
        }
    ?>
  </body>
</html>

【讨论】:

  • 没有逻辑地说,当发布的两个表单的值被发布时,只有第二个存在。
  • 我不确定你在说什么。我在解释 OP 逻辑中的错误是,当他们第一次使用 NF 输入提交表单时,页面会重新加载 2 个表单。所以现在他们可以再次提交表单 1(使用相同或不同的值),或者使用新文件名提交表单 2。但是当提交第二个表单时,&lt;input name = "FileHandling"&gt; 没有提交,因为它只是第一个表单中的一个字段。 OP 用于创建文件的 PHP 逻辑需要 $FileCommand == 'NF'null !== $FileName,但两者都不成立,因为每个表单只有 1 个字段。
【解决方案2】:

当您创建新表单时,由于页面重新加载而导致请求被阻止。 这是解决您的问题的有效代码。

<form method="get" action="index.php">
  <input type="text" name="filehandling">
  <input type="submit" name="createfile">
</form>

<?php

    if ( isset($_GET['createfile']) ) {
        $fh = $_GET['filehandling'];
        if ($fh == 'NF') {
            echo '
                <form method="get">
                    <input type="text" name="filename">
                    <input type="submit" name="createnewfile">
                </form>
            ';
        }
    }
    if ( isset($_GET['createnewfile']) ) {
        $filename = $_GET['filename'];
        $f = fopen($filename, "w");
        fclose($f);
    }

?>

【讨论】:

    【解决方案3】:

    尝试使用is_null() PHP 函数得到想要的结果

    将您的代码更改为

    if(!is_null($FileName)) {
      echo $FileName;
      echo "yes";
      $CreateTheFile = fopen($FileName, 'w');
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多