第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

文件不是用 PHP 創(chuàng)建的?

文件不是用 PHP 創(chuàng)建的?

PHP
慕容3067478 2023-03-26 14:20:28
我用 PHP 編寫了這段代碼,因?yàn)槲蚁刖毩?xí)文件處理。<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”并輸入文件名后出現(xiàn)問題。該文件不會被創(chuàng)建:echo $FileName;if(null !== $FileName) {  echo $FileName;  echo "yes";  $CreateTheFile = fopen($FileName, 'w');}
查看完整描述

3 回答

?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個贊

您處理輸入和表單的方式存在錯誤。目前,您正在檢查$FileCommand == 'NF',這在第一次提交表單時為真。但是隨后頁面重新加載,您將獲得帶有新輸入的第二個表單。所以當(dāng)你填寫第二個表格并重新提交時,現(xiàn)在<input name='FileHandling' />沒有提交,因?yàn)樗皇沁@個表格的一部分(它是第一個表格的一部分)。


因此,如果您將 PHP 更改為以下內(nèi)容,則將嘗試創(chuàng)建文件 if $FileName !== null(無論 的值如何$FileCommand)而不是您之前的邏輯也需要$FileCommand == 'NF'. 這將創(chuàng)建文件的邏輯移到了第一個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. ";

    }

?>

處理這個問題的另一種方法是只創(chuàng)建一個單獨(dú)的字段而不是一個完全單獨(dú)的表單。


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


查看完整回答
反對 回復(fù) 2023-03-26
?
qq_花開花謝_0

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個贊

當(dāng)您創(chuàng)建新表單時,由于頁面重新加載而阻止請求。這是解決您的問題的工作代碼。


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

    }


?>


查看完整回答
反對 回復(fù) 2023-03-26
?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個贊

嘗試使用is_null()PHP 函數(shù)來獲得想要的結(jié)果


將您的代碼更改為


if(!is_null($FileName)) {

  echo $FileName;

  echo "yes";

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

}


查看完整回答
反對 回復(fù) 2023-03-26
  • 3 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號