PHP fopen prepend

Soldato
Joined
21 Apr 2003
Posts
4,328
...as opposed to append.

Basically taking input from a web form, and appending it to a text file, which is being used as a simple guestbook. But I'd like the posts to appear at the top of the text file, instead of the bottom.

Seeing as fopen doesn't support this, is the only way to do this by copying the file into a temporary file, writing the new entry into the main file, then appending the temp file back on?
 
Fat lot of help you all are! :p

I've had a go, but is this still going to be viable as the size of the text file grows?

Code:
			$oldentries = file_get_contents("../guestbook.txt");

			$gBook = "../guestbook.txt";
			$wri = fopen($gBook, 'w') or die("Blaaarruuugghh!");

			$stringData = "<div class=\"gbentry\">\n";
			fwrite($wri, $stringData);
			$stringData = " <i>$date</i>\n <br /> <b>Name:</b> $name\n <br /><b>Subject:</b> $subject\n <br /><b>Message:</b> $text\n<br />\n";
			fwrite($wri, $stringData);
			$stringData = "</div>\n<br />\n";
			fwrite($wri, $stringData);

			fclose($wri);

			$app = fopen($gBook, 'a') or die("Blaaarruuugghh!");
			fwrite($app, $oldentries);

			fclose($app);
 
Oh blimey, two glasses of wine can't cope with all that!

Earlier today though, I did have a good old fiddle with what I had done.

Let me show you - this is not only the 'post to guestbook' part, there's image validation and emailing going on here too:

Code:
<?php

include "settings.php";

session_start();
$string = strtoupper($_SESSION['string']);
$userstring = strtoupper($_POST['userstring']); 
session_destroy();

$date=date('D jS \of F, Y - H:i:s');

if (($string == $userstring) && (strlen($string) > 4)) {

	if ( empty($name) || empty($email) || empty($subject) || empty($text) ) {
		header("Location: $failure");
		exit();
	}
	else {

		$allowedTags='<a><b><i><u>';
		$source = strip_tags($source, $allowedTags);

	        @extract($_POST);
		$name = strip_tags(stripslashes($name), $allowedTags);
		$email = stripslashes($email);
		$subject = strip_tags(stripslashes($subject), $allowedTags);
		$text = strip_tags(stripslashes($text), $allowedTags);

		mail('[email protected]',$subject." [".$book."]",$text,"From: $name <$email>");

		if ( isset($book) ) {

			$text = nl2br($text);

			$oldentries = file_get_contents("../guestbook.txt");

			$gBook = "../guestbook.txt";
			$wri = fopen($gBook, 'w') or die("Blaaarruuugghh!");

			$stringData = "<div class=\"gbentry\">\n";
			fwrite($wri, $stringData);
			$stringData = " <i>$date</i>\n <br /> <b>Name:</b> $name\n <br /><b>Subject:</b> $subject\n <br /><b>Message:</b> $text\n<br />\n";
			fwrite($wri, $stringData);
			$stringData = "</div>\n<br />\n";
			fwrite($wri, $stringData);

			fclose($wri);

			$app = fopen($gBook, 'a') or die("Blaaarruuugghh!");
			fwrite($app, $oldentries);

			fclose($app);
		}
		header("Location: $success");
		exit();
	}

} 

else {
	header("Location: $failure");
	exit();
}

?>

The page it's all for is http://www.sarawallen.com/contact/

Simply, I'm pulling the text file straight into the HTML with a 'require()' function, which is why all that formatting is going in too.

XML, parsing... that's for me to learn another time I think :p
 
Back
Top Bottom