By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

How do I prevent fwrite() overwriting text

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2

P: n/a
Shawn Wilson <sh***@glassgiant.com> wrote in message news:<3F***************@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
Jul 17 '05 #3

P: n/a
Tuuska wrote:

Shawn Wilson <sh***@glassgiant.com> wrote in message news:<3F***************@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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.