为了账号安全,请及时绑定邮箱和手机立即绑定

文件不是用 PHP 创建的?

文件不是用 PHP 创建的?

PHP
慕容3067478 2023-03-26 14:20: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');}
查看完整描述

3 回答

?
www说

TA贡献1775条经验 获得超8个赞

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


因此,如果您将 PHP 更改为以下内容,则将尝试创建文件 if $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>


查看完整回答
反对 回复 2023-03-26
?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

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


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

    }


?>


查看完整回答
反对 回复 2023-03-26
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

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


将您的代码更改为


if(!is_null($FileName)) {

  echo $FileName;

  echo "yes";

  $CreateTheFile = fopen($FileName, 'w');

}


查看完整回答
反对 回复 2023-03-26
  • 3 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信