Page 1 of 2 12 LastLast
Results 1 to 10 of 15

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

  1. #1
    Join Date
    Apr 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.

    But how do you do this?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You would want to use the file system functions in PHP. fopen, fread, fwrite, file_get_contents, and some other ones that you can see for yourself on their website (PHP.net).

    A simple example of how to write to a text file:

    Code:
    <?php
    $filename = 'test.txt';
    $string = "This is some text that will be written to the test file!";
    
    $fp = fopen($filename, 'w');
    fwrite($fp, $string);
    fclose($fp);
    ?>
    Hope this helps.
    Last edited by thetestingsite; 04-06-2007 at 03:13 AM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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

    Default

    In the fopen call, doesn't the handle have to be a string?
    Code:
    $fp = fopen($filename, "w");
    - Mike

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ah yes, typing fast and missed my quotes; although, this will probably still work. Anyways, thanks for pointing that out Mike.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It will still work without quotes (unless there's a define with the name w) but it shouldn't be relied upon, because it requires error-correction to work and would conflict with the aforementioned define if it were introduced.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    i have a script on my home computer that makes a text box containing the text file, you can edit it and click save and it makes the whole file look like the text box did.... i will post it later

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

    Default

    I think the issue is simply writing to a text file, not the whole she-bang.
    thetestingsite has explained what to do, anyways.
    - Mike

  8. #8
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Code:
    <?php
      if ($changefile) {
    $slash = stripslashes($_POST['filetest']);
    $filetochange = "http://site.com/file.txt";
    $filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
    fputs($filetochangeOpen,$slash);
    fclose($filetochangeOpen) or die ("Error Closing File!");
    }
    ?>
     	<form method=post action="">
    	
    <textarea rows="40" cols="60" name="filetest">
    <?
    // Implode CSS
    $filetochange = "http://site.com/file.txt";
    print (implode("",file($filetochange)));
    ?>
    </textarea><br />
    
                    <br />
    	<input type="submit" value="Change File" name="changefile">
    	</form>

  9. #9
    Join Date
    Mar 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    1. You should use implode("\r\n", $filetochange);
    2. You can dump the whole print(implode("\r\n", $filetochange)); for print(file_get_contents($filetochange));

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    1. You should use implode("\r\n", $filetochange);
    You probably mean implode("\r\n", file($filetochange)), but the \r\n is only for Windows: UNIX-based operating systems use just \n, and the old Mac-based operating systems (pre OS X) use just \r.
    2. You can dump the whole print(implode("\r\n", $filetochange)); for print(file_get_contents($filetochange));
    Only in PHP 4.3.0 and later.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •