Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add to a file

Status
Not open for further replies.

FredrikN

Programmer
Jan 5, 2001
60
SE
Hi, I'm writing an simple chat in PHP.

But I have a little problem, I don't know how to add some text to the topp of a text file.
To the end of the file is easy but how can I add some text to the top ??

Thanks
 
When you call the
Code:
fopen()
function, you should do it this way:
Code:
$fp = fopen($file_path, "w");

From the PHP manual: (
Code:
'w' - Open for writing only; 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. 

'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.

In the chat situation, I don't know if you want to read from the file, but with the "w+" parameter, you could. As the manual states, the pointer is at the beginning of the file, so you can just write the line, and it will be at the beginning of the file. Using
Code:
rewind()
will take you to the beginning of the file as well. Example:
Code:
rewind($fp);[/code}
[URL unfurl="true"]http://www.php.net/manual/en/function.rewind.php[/URL]

Enjoy! :-)
 
oops... my
Code:
rewind()
example got messed up. Here:
Code:
rewind($fp);
 
Negative.

Do not use

$fp = fopen($file_path, "w");

Use of "w" or "w+" will erase all information in the file.

If you wish to preserve all the information that is already in the file, use

$fp = fopen($file_path, "a"); ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Note that use of fopen($fp,"a"); will place the file pointer at the BEGINNING of the file and normally ERASES the files contents....

A known bug, not sure if its been fixed or if you still need to use a workaround.
***************************************
Party on, dudes!
[cannon]
 
KarveR, According to whose documentation?

From
'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

And in my experiments (on 4.2.1) "a"-mode will happily append to a file, leaving the contents intact. ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Guess oits been fixed, check the last updated date .. Last updated: Mon, 10 Jun 2002 ***************************************
Party on, dudes!
[cannon]
 
So... FredrikN should open the file this way:
Code:
$fp = fopen($file_path, "w");
rewind($fp);
...and always rewind() before writing to the file.
 
Wait! I should have written that as:
Code:
$fp = fopen($file_path, "a");
rewind($fp);

D'OH!!!
 
sonnysavage, what version of PHP are you running? I'm running PHP 4.2.1 -- the rewind function will not allow me to write to the beginning of a file.

One workaround: write the new data to a new file, then open the old file, read its data and write it to the new file, close both files, unlink the old file, and finally rename the new file to the old file's name.

______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Here is just an idea which might not work.
<?php
$fp = fopen(&quot;filename&quot;, &quot;r+&quot;);
$content = fread($fp, filesize($fp));
rewind($fp);
fwrite($fp, &quot;$newcontent\n$content&quot;);
fclose($fp);
?> //Daniel
 
I'm sorry, I've never actually written to a file with PHP. What you are saying is, using rewind() on a file and then writing to it will overwrite what is currently there? danielhoza's solution looks like a good one then.
 
sonnysavage,

Here's what I know:

1. If you open a file in &quot;w&quot; or &quot;w+&quot; mode, your filelength goes to 0 immediately. Even if you never read or write to it. The act of opening it wipes it out.

2. If you usw &quot;a&quot; or &quot;a+&quot;, then no matter what you do with the file handle, you are writing to the end of the file.

3. danielhozac's code is correct with one edit:
<?php
$fp = fopen(&quot;filename&quot;, &quot;r+&quot;);
$content = fread($fp, filesize(&quot;filename&quot;));
rewind($fp);
fwrite($fp, &quot;$newcontent\n$content&quot;);
fclose($fp);
?>

______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Hmmm .. opening a file with the &quot;a&quot; attribute *should* append, however this was a known bug which has been fixed as I correctly posted.

Read the post thoroughly as it provides HELP.

W or W+ is not the way to append a file.

Do not flame peoples advice, we are all here to help and not all of us check documentation or try things out on behalf of others. ***************************************
Party on, dudes!
[cannon]
 
CHECK THIS OUT!!!
Quoted from a user submitted comments on the fopen() function (
For people new to PHP, like I was several a month ago this will help out with the &quot;functionallity&quot; of fopen(). You can use multiple modes, or how to go about procedures with the file at hand.

Inside the second parameter, the mode, can contain two letters. So if you were to put an &quot;a&quot; and a &quot;w&quot; there, ex.
Code:
$fp = fopen (&quot;text.txt&quot;, &quot;aw&quot;);
it would behave as follows: When you fputs() something to the text.txt it will do everything that &quot;a&quot; does, but it will also carry over some attributes of &quot;w&quot;; In this case it will place the file pointer to the begining of the file, which I find quite handy.

Here is an example of a little script taking advantage of this:
Code:
<?
$fp = fopen (&quot;php.txt&quot;, &quot;aw&quot;);
$fputs (&quot;$name, $date, $title, $news);
fclose ($fp);
?>

This simply will take variables and write them into the *top* of a text file without deleting any of the origional text.

-Peter

PS> PHP ROCKS!


I am going to test this myself, and then write up a FAQ.
 
Daniel's code snippet worked just fine for me. And I'd recommend it for prepending text to a short file.

But his method could cause problems if the original file is large. In that case, you'd probably want to write the new text to a new file, append the old text to it, deleting the old file, then renaming the new file to the old name.

______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top