Search code examples
phphtmlformsfopenfwrite

Trouble Writing back to text file using PHP fwrite from file selected from a dropdown form


I am having trouble writing back to a text file selected from a html form with a dropdown menu, the script is able to read but is unable to write back, returns "Unable to Open File".using PHP fopen and fwrite

This is the index.html code:

<html>
<head></head>
<title>Edit Server Side Text Files</title>
<body>
<form action="function.php" method="post">
<select name="list">
  <option value="./files/banned-kdf.txt">banned-kdf.txt</option>
  <option value="./files/blackList.txt">blackList.txt</option>
</select>
<input type="submit" name="edit" value="Edit" />
</form>
</body>
</html>

And this is the function.php code:

<?php

if(isset($_POST["edit"])) {
   $filename = $_POST["list"];
   global $filename;
//   var_dump($filename);
   $myfile = fopen("$filename", "r") or die("Unable to open file!");
   $filecontent =  fread($myfile,filesize("$filename"));
   fclose($myfile);
 }
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<textarea name="textareaContent" rows="50" cols="100">
<?

if(isset($filecontent)) { echo $filecontent; }

?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
 </textarea>
<?
//write to text file
 if(isset($_POST["update"])) {
// The following two lines are just for troubleshooting and have been commented out.     
//   echo $_POST["update"];
//   var_dump($filename);
   $myfile = fopen("$filename", "w") or die("Unable to open file!");
   fwrite($myfile, $_POST["textareaContent"]);
   fclose($myfile);
 }
 ?>

 </form>
 <br>
 <br>
<a href="/index.html">Return to List Selection</a>
</html>

Solution

  • You're not sending file name in the second form, just code, so, $filename is undefined. Add it in an input hidden:

    global $variable; is not useful here, because the variable will not be present on next script execution, when processing the form.

    <?php
    
    // Default value for contents:
    $filecontent = '';
    
    // Ok, read the file here
    if(isset($_POST["edit"])) {
       $filename = $_POST["list"];
    
       global $filename; // This will not be useful
    
       $myfile = fopen("$filename", "r") or die("Unable to open file!");
       $filecontent =  fread($myfile,filesize("$filename"));
       fclose($myfile);
     }
    // If request comes from second form, process it
    if(isset($_POST['update'])) {
       // Get file name from input hidden
       $filename = $_POST["filename"];
    
       $myfile = fopen("$filename", "w") or die("Unable to open file!");
       fwrite($myfile, $_POST["textareaContent"]);
       fclose($myfile);
       // End the script, no need to show the form again
       die('File updated successfully.');
    }
    ?>
    <html>
    <form action="<?php echo $PHP_SELF;?>" method="post">
    <input type="hidden" name="filename" value ="<?php
        // Send filename within form
        echo $filename;
    ?>">
    <textarea name="textareaContent" rows="50" cols="100">
    <?
    // No if needed here, we already have a value
    echo $filecontent;
    ?>
    </textarea>
    <br>
    <br>
    <input type="submit" name="update" value="Update" />
    
     </form>
     <br>
     <br>
    <a href="/index.html">Return to List Selection</a>
    </html>