How do I prevent fwrite() overwriting text

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default How do I prevent fwrite() overwriting text

    Hello!

    I have this problem, when fwrite() writes to the beginning of xyz.txt
    file, it overwrites the first line. Any ideas how to prevent this? I'm
    running PHP 4.3.3RC3 on Linux.

    <?php
    $fp = fopen("xyz.txt","r+");
    fwrite ($fp,"$newline");
    fclose ($fp);
    ?>

    -Tuuska
    Tuuska Guest

  2. Similar Questions and Discussions

    1. Text overwriting itself
      The website in question is: http://www.medialabargentina.com and I'm using the Flash Linux plugin on Debian Sarge (with Epiphany, Mozilla,...
    2. Q: prevent text flow around image?
      hi everyone, i am working with dynamic text fields, loading external xml etc. it works fine, except for the fact that i can't figure out how to...
    3. fwrite text problem
      I need some ideas as to why this may have happened. I have a form up on a friend's web site that upon posting will fwrite the information submitted...
    4. prevent returns in a text field
      Is there a way to prevent "returns" inside of an editable text field? I've already limited the amount of characters. I just don't want the text...
    5. how to prevent prevent .so-calling routine to crash from segfaults in .so
      Hi, Guys I am stuck with a problem and need some help. Platform : Linux(RedHat 7.3) Problem Area : Dynamic Shared Object Libraries, POSIX...
  3. #2

    Default Re: How do I prevent fwrite() overwriting text

    Tuuska wrote:
    >
    > Hello!
    >
    > I have this problem, when fwrite() writes to the beginning of xyz.txt
    > file, it overwrites the first line. Any ideas how to prevent this? I'm
    > running PHP 4.3.3RC3 on Linux.
    >
    > <?php
    > $fp = fopen("xyz.txt","r+");
    > fwrite ($fp,"$newline");
    > fclose ($fp);
    > ?>
    >
    > -Tuuska
    You could try appending ( use "a" instead of "r" ). That'll put you new text at
    the end of the file. If it has to do at the beginning, you'll have to read the
    file into memory, write your new text, then write the rest of the file from
    memory. Or you could open a new, temporary file, write to it, read your
    existing file and write to the temp file chunk by chunk. Then copy the temp
    file over the existing file.

    Shawn
    --
    Shawn Wilson
    [email]shawn@glassgiant.com[/email]
    [url]http://www.glassgiant.com[/url]
    Shawn Wilson Guest

  4. #3

    Default Re: How do I prevent fwrite() overwriting text

    Shawn Wilson <shawn@glassgiant.com> wrote in message news:<3FC511BE.EC23EC13@glassgiant.com>...
    > Tuuska wrote:
    > >
    > > Hello!
    > >
    > > I have this problem, when fwrite() writes to the beginning of xyz.txt
    > > file, it overwrites the first line. Any ideas how to prevent this? I'm
    > > running PHP 4.3.3RC3 on Linux.
    > >
    > > <?php
    > > $fp = fopen("xyz.txt","r+");
    > > fwrite ($fp,"$newline");
    > > fclose ($fp);
    > > ?>
    > >
    > > -Tuuska
    >
    > You could try appending ( use "a" instead of "r" ). That'll put you new text at
    > the end of the file. If it has to do at the beginning, you'll have to read the
    > file into memory, write your new text, then write the rest of the file from
    > memory. Or you could open a new, temporary file, write to it, read your
    > existing file and write to the temp file chunk by chunk. Then copy the temp
    > file over the existing file.
    >
    > Shawn
    Hello!

    Thanks for your tip! I managed to make it work! First it reads the
    content of txt file into $contents and then writes $newline and
    eventually writes $contents to the end of the txt file.

    Probably not the best way to do this, but it's enough for me.

    <?php
    $filename = "xyzt.txt";
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);
    ?>

    <?php
    $fp = fopen("xyz.txt","w+");
    fwrite ($fp,"$newline");
    fclose ($fp);
    ?>

    <?php
    $fp = fopen("xyz.txt","a");
    fwrite ($fp,"$contents");
    fclose ($fp);
    ?>

    -Tuuska
    Tuuska Guest

  5. #4

    Default Re: How do I prevent fwrite() overwriting text

    Tuuska wrote:
    >
    > Shawn Wilson <shawn@glassgiant.com> wrote in message news:<3FC511BE.EC23EC13@glassgiant.com>...
    > > Tuuska wrote:
    > > >
    > > > Hello!
    > > >
    > > > I have this problem, when fwrite() writes to the beginning of xyz.txt
    > > > file, it overwrites the first line. Any ideas how to prevent this? I'm
    > > > running PHP 4.3.3RC3 on Linux.
    > > >
    > > > <?php
    > > > $fp = fopen("xyz.txt","r+");
    > > > fwrite ($fp,"$newline");
    > > > fclose ($fp);
    > > > ?>
    > > >
    > > > -Tuuska
    > >
    > > You could try appending ( use "a" instead of "r" ). That'll put you new text at
    > > the end of the file. If it has to do at the beginning, you'll have to read the
    > > file into memory, write your new text, then write the rest of the file from
    > > memory. Or you could open a new, temporary file, write to it, read your
    > > existing file and write to the temp file chunk by chunk. Then copy the temp
    > > file over the existing file.
    > >
    >
    > Thanks for your tip! I managed to make it work! First it reads the
    > content of txt file into $contents and then writes $newline and
    > eventually writes $contents to the end of the txt file.
    >
    > Probably not the best way to do this, but it's enough for me.
    Just a few tips/cautions. The way it's written now should work fine as long as
    the original file is small and will stay small. You may run into trouble if you
    get a very large file.

    Depending on the application, you may want to consider making parts 2 and 3
    conditional upon part 1 being completed successfully. Otherwise the original
    file will not be written into the new file - you'll just have $newline in there.

    There's really no need to close the file at the end of part 2 and reopen it at
    the beginning of part 3. Those could be done with a single fopen() and 2
    fwrite()s. It may be that you've done it this way because some code that you've
    snipped out requires it. If so, ignore this.

    You may want to consider changing your coding to something like the following
    (UNTESTED):

    $filename = "xyzt.txt";
    if ($fd = @fopen ($filename, "r")) {
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);
    }
    else
    echo("Could not open the file <i>$filename</i>");

    Otherwise, if there's an error opening the file, it will be displayed, along
    with an error for the fread, and an error for the fclose(). While you're
    testing, though, don't use the "@". You want errors displayed while you're
    testing.

    //PART 1
    > <?php
    > $filename = "xyzt.txt";
    > $fd = fopen ($filename, "r");
    > $contents = fread ($fd, filesize ($filename));
    > fclose ($fd);
    > ?>
    //PART 2
    > <?php
    > $fp = fopen("xyz.txt","w+");
    > fwrite ($fp,"$newline");
    > fclose ($fp);
    > ?>
    //PART 3
    > <?php
    > $fp = fopen("xyz.txt","a");
    > fwrite ($fp,"$contents");
    > fclose ($fp);
    > ?>
    Regards,
    Shawn

    --
    Shawn Wilson
    [email]shawn@glassgiant.com[/email]
    [url]http://www.glassgiant.com[/url]
    Shawn Wilson Guest

  6. #5

    Default How do I prevent fwrite() overwriting text

    $fp = fopen('details2.txt', 'a');
    fwrite($fp, $content);
    fclose($fp);

    If you append the content, then it is very simple.
    ramasamy Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139