-1

I've made this code in PHP because I want to practice file handling.

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

However, there is a problem after you choose 'NF' and type in the file name. The file does not get created:

echo $FileName;
if(null !== $FileName) {
  echo $FileName;
  echo "yes";
  $CreateTheFile = fopen($FileName, 'w');
}
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
RtHAitP30D
  • 155
  • 1
  • 3
  • 12

3 Answers3

1

You have a bug in the way you're handling inputs and forms. Currently, you're checking $FileCommand == 'NF', which is true on the first form submission. But then the page reloads and you get a second form with a new input. So when you fill in the second form and resubmit it, now <input name='FileHandling' /> wasn't submitted because it wasn't part of this form (it's part of the first form).

So if you change your PHP to the following, the file will be attempted to be created if $FileName !== null (regardless of the value of $FileCommand) rather than your previous logic which also required $FileCommand == 'NF'. This moves the logic for creating the file outside of the first 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. ";
    }
?>

Another way to handle this would be to create only a separate field instead of a completely separate form.

<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>
WOUNDEDStevenJones
  • 4,003
  • 5
  • 37
  • 46
  • There is no logic in saying that when the values of the two forms posted are posted only second exists. – Kunal Raut May 15 '20 at 21:27
  • I'm not sure what you're saying here. I'm explaining that the bug in OP's logic is that when they first submit the form with `NF` input, then the page reloads with 2 forms on it. So now they could either submit form 1 again (with the same or a different value), or submit form 2 with the new filename. But when the second form is submitted, `` isn't submitted because it's only a field in the first form. OP's PHP logic to create a file requires `$FileCommand == 'NF'` and `null !== $FileName`, but both won't be true because there's only 1 field per form. – WOUNDEDStevenJones May 15 '20 at 21:33
0

When you create new form get request blocked because of page reload. This is working code to solve your problem.

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

?>
-1

Try to use is_null() PHP function to get the desired result

Change your code to

if(!is_null($FileName)) {
  echo $FileName;
  echo "yes";
  $CreateTheFile = fopen($FileName, 'w');
}
Kunal Raut
  • 2,098
  • 2
  • 6
  • 23
  • This doesn't actually solve the problem at all. [There is no logical difference](https://stackoverflow.com/questions/8228837/is-nullx-vs-x-null-in-php) between your suggestion and what OP has. – WOUNDEDStevenJones May 15 '20 at 21:17