View Full Version : Reading files backwards in PHP
SneakyDave
04-15-1999, 11:21 AM
The answer to this question is probably similar to many "languages", but I'm trying to do it in PHP, so I'm offering it here. And I'm sure its easy with MySQL, but I'm too poor for it (My mother always said "You're too poor to pay attention").
I have a guestbook application that simply writes a user name, email, and message to a data file.
Normally, looking at the message posts, they appear the earliest on the top to the most recent on the bottom. I want to change the order, but I don't want to have to copy the entire file to a temp file, initialize a new data file, write the new post, and then copy the records from the temp file to the new and improved data file.
I've tried using the fopen with the "r+" mode thinking that I could position the pointer at the beginning of the file, write the new post, and then read and write the remaining posts, but it doesn't seem to work as I expected.
Anybody got any ideas? Thanks
Sneaky
Justin
04-15-1999, 08:46 PM
I'll admit, due to using MySQL in php, I don't know the first thing about using text files in php http://www.aota.net/ubb/smile.gif However, in Perl, the way I'd do it (although it's probably not the best way) is to suck the whole file into a string (array) and basically insert the newest entry. Then just rewrite the file.
If it's HTML, most guestbooks do something like this:
<!-- START -->
Or similar, so that it can flag where to insert the next post. Then the script will go through it line by line like this:
[Ucode]
open (FILE,"path/to/file.html");
@stuff = <FILE>;
close (FILE);
$stuff = join('',@stuff);
chomp($stuff);
@stuff = split(/\n/,$stuff);
open (FILE,">path/to/file.html");
foreach $line(@stuff) {
if ($line =~ /<!-- START -->/) {
print FILE "<!-- START -->\n";
print FILE "Name: $name
\n";
print FILE "Email: $email
\n";
print FILE "Comments:\n";
print FILE "<blockquote>$message</blockquote>\n<hr>\n\n";
} else {
print FILE "$line\n";
}
}
close (FILE);
print "Location: http://url.to.file.html\n\n";
exit;
[/quote]
But of course we want php... so it's going to be different...
Ok, how's this: Get the file into a string. Then make the new entry in a string. Then try this:
ereg_replace("<!-- START -->",$NewEntry,$File);
Assuming the new entry, including the <!--START --> as the first line, is in the string $NewEntry. Then print the string $File out to your HTML file...
Let me know if this helps. I'm curious, because I've never worked with text files in php before, although I really don't see a guestbook that warrants a MySQL Database http://www.aota.net/ubb/smile.gif
------------------
Justin Nelson
FutureQuest Support
[This message has been edited by Justin (edited 04-15-99).]
SneakyDave
04-16-1999, 12:11 AM
Justin, can I send you the text I'm using currently to process my text file? Its pretty straight forward, and although I haven't done a lot of flat file processing with PHP myself, it seems that it may be easier to accomplish (although technically the same) than Perl. I'm looking through the PHP manuals, and I can't even find a good flock() example, so I'm guessing my version works because I'm not getting a "bad" return code (as far as I know).
Catchya on the flip side.
Sneaky
SneakyDave
04-16-1999, 01:43 AM
Ok, I finally got it to work, after trying everything JUST to get a file in an array, and then JACKING with the code to get it to work right...
I learned a very important lesson after 3 hours of frustration: don't try to end the first line of a WHILE statement with a semi-colon, I had to use a regular colon. Don't ask me why, probably a very good reason, but my apologies to FQ for the "maximum time limit exceeded" errors using it.
Justin, let me know, and I'll send you the source I used in case you want to try using it in the future, or give some opinion on a better way to do it.
Thanks again.
Sneaky.
My mother always said "Don't start, and you won't have to quit"
She never claimed to be very ambitious.
Terra
04-16-1999, 02:57 AM
I am assuming a text file of:
1
2
7
8
9
and you want to 'inject' '3,4,5,etc' between 2 and 7...
Pseudo-Code:
open file
walk file w/while
trigger on 2
snag current File Pointer
slurp rest of file into array until EOF
seek to saved FP
truncate to saved FP
write new data
write array
close file
The above is a balance of convenience and lower memory consumption, as the entire file does not need to be read into memory... It's also very fast as random seeks are quick with the positioning calls...
When you are writing secure code, you *never* want to create intermediate temporary files, as this opens up 'mode' and 'race' possible exploits... Always try to work on that single file using the seek and truncation capabilities...
In Perl, it's trivial - in PHP3 I don't have a clue yet, but I'm sure it has that full functionality as positioning are fundamental file building block capabilities... http://www.aota.net/ubb/wink.gif
--
Terra
--Hey buddy, don't be cutting in line!--
FutureQuest
Justin
04-16-1999, 05:10 AM
Send it on over if you want. I have been wanting to get into the idea of generating HTML or better yet PHP pages from PHP http://www.aota.net/ubb/smile.gif Would be cool to see what you've got so far.
------------------
Justin Nelson
FutureQuest Support
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.