php: append data to the beginning of a file?

Need help with C, C++, perl, python, etc?

php: append data to the beginning of a file?

Postby simen » 2006-12-07 21:24

I've been learning php for a few days, using it among other things to handle comments from visitors on a web page. I store the comments in a file. Right now, the relevant part of my script looks like this:

$file=fopen("$path","a");
fwrite($file,$comment."\n");
fclose($file);

As you might imagine, $path contains the path to the comment file. $comment contains data from a form, formatted to look nice in an html file. To display the comments, I just do "include($path);", and get a view of all the comments in chronological order. However, I'd like to reverse that order, so that the latest comments are shown first, with older and older comments coming further down the page. In other words: instead of adding $comment to the end of $file, I'd like to add it to the beginning. I've tried switching around the parameters for fopen(), but the best I've managed is to overwrite from the start of $file, or trunkate the whole thing. Obviously, I've also looked at the documentation, but I haven't found anything like what I'm looking for. I could of course just read the entire file to a string, add the new $comment to the beginning of that string and then overwrite the file with it[1]. Seems like a very roundabout way to do a quite simple operation.

So: what's the nicest way to add some data to the beginning of a file without overwriting or deleting anything in the file?

Regards,
--Simen

[1] EDIT: In other words something like this:

$file=fopen("$path","r+b");
$oldcontents=fread($file, filesize($path));
$newcontents=$comment."\n\n".$oldcontents;
rewind($file);
fwrite($file,$newcontents);
fclose($file);

That does the job, but it seems to me (a very unskilled programmer) that I'm passing too much data back and forth here?
User avatar
simen
 
Posts: 206
Joined: 2005-07-02 15:00
Location: Berlin

Re: php: append data to the beginning of a file?

Postby plugwash » 2006-12-07 22:44

simen wrote:but I haven't found anything like what I'm looking for.

not surprising
I could of course just read the entire file to a string, add the new $comment to the beginning of that string and then overwrite the file with it[1]. Seems like a very roundabout way

thats the simplest way and what i'd do if i new the file was going to remain relatively small. you can get into more complex systems that will save memory but its not worth it for small files.

to do a quite simple operation.

filesystems really aren't built to add stuff to the beggining of files, adding stuff to the end of a file basically consists of "read the last sector back into your output buffer and then start adding stuff and flushing it out to sectors in the normal way". adding stuff to the start of a file means moving the stuff thats already there down the file.
plugwash
 
Posts: 2508
Joined: 2006-09-17 01:10

Postby simen » 2006-12-07 23:37

Thanks for clarifying that, plugwash. I'll just go with the read-concatenate-write-solution, then, suspecting that the comments file is going to remain pretty small ;)

Regards,
--Simen
User avatar
simen
 
Posts: 206
Joined: 2005-07-02 15:00
Location: Berlin

Postby TonyT » 2006-12-14 22:56

A mysql db would make it more robust, you could store user comments in tables, query them to display them and sort the order easily.
TonyT
 
Posts: 574
Joined: 2006-09-04 11:57


Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable