append to beginning of file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

append to beginning of file

Post by oculasm »

[newbie alert :oops: ]
hi everyone this is a really simple issue..(I hope)


:?: I need to write the contents of a post operation to the beginning of a file... its like two tiny form fields. nothing fancy.

I guess appending is out of the question, its more like prepending without overwriting any of the existing contents. r+ seems to overwrite bits but starts writing at the right place, the beginning of the file.


I have tried as best as I can to find an answer online .. ironically the only place I havent been able to visit is php.net! - its refusing my connection??

Im not really keen on any complicated methods of doing this..like reversing arrays and stuff. I tried them to no avail..

there must be a simple way that I have overlooked.
Ive tried all the switches.. a+ r+ w+ and the rewind function too,


any suggestions/code corrections would be much appreciated..

Code: Select all

<?php
@extract($_POST);
if(is_writable('news.txt'))
{

$fp = fopen('news.txt','r+');
$content = "\n$entrydate\n$entrymsg";


fseek($fp, 0, SEEK_SET); // not sure what the seek set means or that 0
//assuming that this puts the pointer at byte 0 or something?

//fwrite($fp,$content);  // thought it might have issues with this.
fputs ($fp, "\n$entrydate\n$entrymsg");
fclose($fp);
echo'Successfully written entry';
}
else
{
echo'File is not writable';
}
?>
the code above just keeps appending to the end of the file...



cheers
-i
_________
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php
$newStuff = "Some new stuff";

$file = "oldStuff.txt";
$o = fopen($file, "r+b");
$oldStuff = fread($o, filesize($file));
$newStuff .= $oldStuff;
fwrite($o, $newStuff);
fclose($o);
?>
......... erm........ that should do the trick but you'll need to test it :)
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

hmm.. no unfortunately that seems to strangely append left right and centre... the appended text ends up being in the middle.. so I assume it writes above it as well. cheers for replying.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Take a look at the manual, I'm sure the problem has got something to do with which tags you use to open the file..... r+b might not be the right one to use.

fopen($file, "r+b");
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

yeah I think I will keep playing with it...

its just so surprising that a solution to appending at the beginning of the file is so hard to find.

if anyone else has encountered the same issue before or knows a solution please enlighten me.

thanks Gen-ik
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Well, one that looks like it would work, although a little different, might be:

Code: Select all

<?php
$filearray[] = "Some new stuff"; 

$file = "oldStuff.txt"; 
$oldarray = file($file);

$filearray[] = $oldarray;

$stream = implode("\n", $filearray);
$o = fopen($file, "w"); 

fwrite($o, $stream); 
fclose($o); 
?>
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Looks like you could also do:

Code: Select all

<?php 
$newstring = "Some new stuff"; 

$file = "oldStuff.txt"; 
$oldstring = file_get_contents($file); 

$newstring .= oldstring; 

$o = fopen($file, "w"); 

fwrite($o, $newstring); 
fclose($o); 
?>
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

logically it makes every bit of sense.. I tried your code

Code: Select all

<?php

$filearray[] = "rrrrrrrr"; 

$file = "news.txt";
$oldarray = file($file); 

$filearray[] = $oldarray; 

$stream = implode("\n", $filearray); 
$o = fopen($file, "w"); 

fwrite($o, $stream); 
fclose($o); 


?>



and this was the result

rrrrrrrr
Array
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Ooops, change the line:

Code: Select all

$filearray[] = $oldarray;
to

Code: Select all

$filearray = array_merge($filearray,$oldarray);
User avatar
oculasm
Forum Newbie
Posts: 20
Joined: Sat Jul 19, 2003 2:26 pm

Post by oculasm »

wow thanks stoneguard.It works
you certainly deserve a a corny joke I hope you havent heard it before:

Q. what did spock find in the restroom! :roll: Answer below the code

Code: Select all

<?php
$filearray[] = "this line will be added to the top of the file whohooo"; 

$file = "news.txt";
$oldarray = file($file); 

$filearray = array_merge($filearray,$oldarray); 


$stream = implode("\n", $filearray); 
$o = fopen($file, "r+"); 

fwrite($o, $stream); 
fclose($o); 
?>


Joke Reply:
A. The Captains Log
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

omg, roflmao. I don't think I have heard that before. Definitely corny.

Funny how there was never a men's bathroom urinal shot where Spock and Kirk were standing side by side discussing the new ensign and the way she holds her clipboard.

Btw, I don't know if you tried the other solution I found, but I think it might be a little more efficient if it works.
Post Reply