Updating text file using PHP

Discussion in 'PHP' started by aquasonic, Apr 25, 2007.

  1. #1
    Is it possible to have a PHP page that takes the contents of a specified text document and allows you to edit that document online without having to go in through FTP?

    If it is, thats brilliant! Let me know,

    Thanks in advance
     
    aquasonic, Apr 25, 2007 IP
  2. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    // Name of the text file - must be chmoded to 777.
    $file = "text.txt";
    
    @touch($file);
    if (!is_readable($file) || !is_writable($file)) {
       die("Chmod $file to 777.");
    }
    
    // Save text file contents if given
    if (isset($_POST["textarea"]) && $textarea = $_POST["textarea"]) {
    
       if (get_magic_quotes_gpc()) {
          $textarea = stripslashes($textarea);
       }
    
       $fp = fopen($file, "w");
       fwrite($fp, $textarea);
       fclose($fp);
    }
    
    // Load text file contents
    $contents = file_get_contents($file);
    
    ?>
    <form action="editor.php" method="POST">
    
    <div align="center">
    <textarea name="textarea" cols="60" rows="10"><?php echo $contents ?></textarea><br />
    <input type="submit" value="Save File" />
    </div>
    
    </form>
    PHP:
     
    Robert Plank, Apr 25, 2007 IP
  3. aquasonic

    aquasonic Well-Known Member

    Messages:
    90
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    115
    #3
    Thank you,

    No one else need reply - got what I needed to know!
     
    aquasonic, Apr 25, 2007 IP