Ask a Question related to PHP Development, Design and Development.
-
Tuuska #1
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
-
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,... -
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... -
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... -
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... -
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... -
Shawn Wilson #2
Re: How do I prevent fwrite() overwriting text
Tuuska wrote:
You could try appending ( use "a" instead of "r" ). That'll put you new text at>
> 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
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
-
Tuuska #3
Re: How do I prevent fwrite() overwriting text
Shawn Wilson <shawn@glassgiant.com> wrote in message news:<3FC511BE.EC23EC13@glassgiant.com>...
Hello!> 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
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
-
Shawn Wilson #4
Re: How do I prevent fwrite() overwriting text
Tuuska wrote:
Just a few tips/cautions. The way it's written now should work fine as long as>
> 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.
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//PART 2> <?php
> $filename = "xyzt.txt";
> $fd = fopen ($filename, "r");
> $contents = fread ($fd, filesize ($filename));
> fclose ($fd);
> ?>//PART 3> <?php
> $fp = fopen("xyz.txt","w+");
> fwrite ($fp,"$newline");
> fclose ($fp);
> ?>Regards,> <?php
> $fp = fopen("xyz.txt","a");
> fwrite ($fp,"$contents");
> fclose ($fp);
> ?>
Shawn
--
Shawn Wilson
[email]shawn@glassgiant.com[/email]
[url]http://www.glassgiant.com[/url]
Shawn Wilson Guest
-
ramasamy #5
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



Reply With Quote

