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

Thread: how do i modify existing .txt files with php?

  1. #1
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i modify existing .txt files with php?

    i need to add the value of a text box to a txt file on my server which will then be included in a textarea on the page. how will i do this (i know how to do the include bit)? thanks

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

    Default

    PHP Code:
    <?php
    $fn 
    "file.txt";
    $file fopen($fn"a+");
    $size filesize($fn);

    if(
    $_POST['addition']) fwrite($file$_POST['addition']);

    $text fread($file$size);
    fclose($file);
    ?>
    <form action="<?=$PHP_SELF?>" method="post">
    <textarea><?=$text?></textarea><br/>
    <input type="text" name="addition"/>
    <input type="submit"/>
    </form>
    That ought to work.
    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!

  3. #3
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey,

    Nice code. How would it need to be changed so that instead of 'adding' text to the .txt file, it actually 'replaced' the text in the .txt file?

    kind regards,

    June

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

    Default

    Change the "a+" to "w+" in fopen().
    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!

  5. #5
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Change the "a+" to "w+" in fopen().
    Produces the error message:

    Warning: fread(): Length parameter must be greater than 0

  6. #6
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    r+ seems to work without an error.

    Your link says the following:

    'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

    I would like to get w+ working.
    Last edited by June; 02-21-2006 at 12:28 AM.

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

    Default

    Remove the line with the fread() call -- as you're removing all the contents of the file, it doesn't make sense to try to read in said contents.
    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!

  8. #8
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey, firstly many thanks for your help.

    Here's what I'm trying to do... use the code as a basic 'text file editor'. The text file is then used as an 'include' in a single webpage. Here's the code.

    Code:
    <?php 
    $fn = "textfile.txt"; 
    $file = fopen($fn, "w+"); 
    $size = filesize($fn); 
    
    if($_POST['update']) fwrite($file, $_POST['update']); 
    
    //$text = fread($file, $size); 
    fclose($file); 
    ?> 
    <form action="<?=$PHP_SELF?>" method="post"> 
    <textarea Name="update" cols="50" rows="10"><?=$text?></textarea><br/> 
    
    <input type="submit" value="Update"/> 
    
    </form>

    By putting // in front of

    $text = fread($file, $size);

    it now doesn't load the text into the textarea from the text file (which it would need to do to edit the text).

    It needs to load the text into the textarea, and be able to replace the content in the text file. At the moment it does one or the other.

    If we could then put a WYSIWYG editor on the textarea such as
    http://www.dynamicdrive.com/dynamici...itor/index.htm

    Then it would make a really fantastic basic CMS.

    Also on submit perhaps the form could reload the current form page (so showing the updated text in the textarea), or go to another page?

    What do you think?
    Last edited by June; 02-22-2006 at 02:37 AM.

  9. #9
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    <?
    if($_POST['Submit']){
    $open fopen("textfile.txt","w+");
    $text $_POST['update'];
    fwrite($open$text);
    fclose($open);
    echo 
    "File updated.<br />"
    echo 
    "File:<br />";
    $file file("textfile.txt");
    foreach(
    $file as $text) {
    echo 
    $text."<br />";
    }
    }else{
    $file file("textfile.txt");
    echo 
    "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo 
    "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
    foreach(
    $file as $text) {
    echo 
    $text;

    echo 
    "</textarea>";
    echo 
    "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
    </form>"
    ;
    }
    ?>
    Last edited by Pixelcode; 02-22-2006 at 09:36 AM.

  10. #10
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Pixelcode
    PHP Code:
    <?
    if($_POST['Submit']){
    $open fopen("textfile.txt","w+");
    $text $_POST['update'];
    fwrite($open$text);
    fclose($open);
    echo 
    "File updated.<br />"
    echo 
    "File:<br />";
    $file file("textfile.txt");
    foreach(
    $file as $text) {
    echo 
    $text."<br />";
    }
    }else{
    $file file("textfile.txt");
    echo 
    "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo 
    "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
    foreach(
    $file as $text) {
    echo 
    $text;

    echo 
    "</textarea>";
    echo 
    "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
    </form>"
    ;
    }
    ?>

    That's great!

    1. On the results page showing what has been submitted (not the text file) it needs a couple of links under the text (because re-entering the url is showing the results and not the form). So to the above, how would I add?

    Code:
    <p><a href="./livepage.php">click here to view the live updated webpage</a></p>
    <p><a href="./mainadminpage.php">click here to view the admin menu</a></p>
    2. Needs to include a free WYSIWYG editor on the text area. Anyone able to do that?

    And that would be it to make a great little CMS.

    Thanks,

    June

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
  •