Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Is it possible to edit a text file via PHP?

  1. #11
    Join Date
    Dec 2006
    Location
    http://andersmoen.com
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, you can. This is really simple.

    The file you want to write to: (call it something.txt)
    Code:
    Here it just says some random words, and yeah... this is just for filling out some, hehe.
    The file where you write something new to that file:
    Code:
    Over the doctype:
    <?php
    if (isset($_POST['submit'])) {
    
    $myFile = "something.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = stripslashes($_POST['sf']);
    fwrite($fh, $stringData);
    fclose($fh);
    
    header('Location: the_file_where_you_want_to_edit_the_textfile.php?a=done');
    
    }
    ?>
    
    Inside body:
    <form action="" method="post">
    <textarea name="sf" cols="40" rows="6">
    <?php
    $myFile = "something.txt";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    ?></textarea>
    <br />
    <input type="submit" name="submit" value="Edit" />
    </form>
    
    <?php
    if ($_GET['a'] == 'done') {
    echo 'The file was saved and now it says:<br /><br />';
    
    $myFile = "something.txt";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    
    }
    ?>

    Remember to change the CHMOD for the txt file to 777 - or don't you have to do that on txt-files?


    Tell me if it doesn't work
    Last edited by andersmoen; 04-15-2007 at 11:33 AM. Reason: I forgot the write code itself, hehe.

  2. #12
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I don't understand why you're redirecting to "the_file_where_you_want_to_edit_the_textfile", seeings you are on the file that's editing the textfile.

    It should work though, but this is nothing that hasn't been mentioned before.
    - Mike

  3. #13
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to edit a text file via PHP?

    Quote Originally Posted by rctxtreme View Post
    I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.

    But how do you do this?
    Is it possible to edit a text file via PHP?

  4. #14
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    http://php.net/manual/en/function.file-put-contents.php
    Make sure the file you are editing is CHMOD'ed to 777!
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  5. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Please start a NEW discussion for a NEW question. I'm going to close this. If you need more help, start a new thread and please don't post in a 3-year-old thread unless it is a direct response to the topic.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •