PHP help

Discuss various technical topics not related to Mozilla.
Post Reply
User avatar
nilson
Posts: 4100
Joined: February 15th, 2003, 11:55 pm
Location: Tuscaloosa, Alabama
Contact:

PHP help

Post by nilson »

I've been working on making a database-less blog (uses a text file to store posts). I tried to make a (very) simple PHP script to allow me to post to it, and it works except for one thing that's making it un-usable for me: I can not figure out how to append to the top (beginning) of a file.

If I use the mode "r+" for fopen, it places the pointer at the beginning of the file, but it over-writes everyting. I need to place text at the beginning of the file (by placing the pointer there), but the text following it must move down (reflow in browser terms).

Here is the main file. It includes a stylesheet (which is not important) and a file pass.php (which simply defines $setpass as the correct password).

Code: Select all

<?php

//By Nilson Cain//

//For posting to the blog//

//Require Authentication//

require('./pass.php');

$pass = $_POST['pass'];
$title = $_POST['title'];
$message = $_POST['message'];

if($pass != $setpass || !$pass)
{
$title="";
$message="";
print <<<END
<html><head><link rel="stylesheet" type="text/css" href="main.css" /></head><body>
<form action="post.php" method="post">
Password: <input type="password" name="pass" size="20" /><br/>
<input type="submit" value="OK" /><br/>
</form>
</body></html>
END;
}

else if($pass = $setpass)
{
print <<<END
<html><head><link rel="stylesheet" type="text/css" href="main.css" /></head><body>
<form action="post.php" method="post">
<input type="hidden" name="pass" value="$pass" />
Title: <input type="text" size="20" name="title" /><br/>
Blog: <textarea name="message" rows="8" cols="30"></textarea><br/>
<input type="submit" value="OK">
</body></html>
END;
}

else{
echo "ERROR";
}

if($pass = $setpass && $title && $message)
{
$filename = 'posts.txt';
$open = fopen ($filename, "a");
$dateid=date("Fd");
$fulldate=date("l, F d, Y");
$content = "<div class=\"header\"><span class=\"title\">$title</span><span class=\"date\" id=\"$dateid\">$fulldate</span></div>\n<p>$message</p>\n";
fwrite($open, $content);
}
?>


I know that the code is very simplistic as of right now (features to come later) but does anyone know any way to solve my problem? Thanks :)

BTW, the site that this is for is located @ http://nilsonscorner.dyndns.org/.
old jasonb
Moderator
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Re: PHP help

Post by old jasonb »

nilson wrote:I can not figure out how to append to the top (beginning) of a file.

The simplest solution off the top of my head (although perhaps not the correct solution - and probably not the simplest if there is a correct one) is to just write out the new data to another file, append the old data to the end of the new file, then delete and rename things. Not very elegant but it should at least be a quick fix until you can figure out if there is a more proper way of doing this.

Alternatively, have each post go to a different file, numbered sequentially, so you don't need to worry about modifying one (soon to become) very big file. You can store the "post number counter" in a separate file and increment it as needed. This latter method would also make it easier for editing and commenting routines.
Last edited by old jasonb on September 14th, 2003, 7:39 pm, edited 1 time in total.
User avatar
nilson
Posts: 4100
Joined: February 15th, 2003, 11:55 pm
Location: Tuscaloosa, Alabama
Contact:

Post by nilson »

But how should I go about sequentially numbering these files automatically? Of course, with a counter as you said, but not sure where to start with this.
old jasonb
Moderator
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by old jasonb »

Simply have a file named "counter.txt" (or whatever) that has a number in it. Whenever you have a new post, read the value into a variable (I'll assume that "fread" would be the logical command), increment it by one, write the new number back out to the counter file again (in this case "r+" would overwrite the old value doing just what you want), and use the new number in the creation of the next post file.
User avatar
nilson
Posts: 4100
Joined: February 15th, 2003, 11:55 pm
Location: Tuscaloosa, Alabama
Contact:

Post by nilson »

Simple enough, but the issue here is that I would like to have one include statement on the page that the blog is to be displayed on.
User avatar
Orbite
Posts: 3757
Joined: November 4th, 2002, 8:23 pm
Contact:

Re: PHP help

Post by Orbite »

nilson wrote:I've been working on making a database-less blog (uses a text file to store posts). I tried to make a (very) simple PHP script to allow me to post to it, and it works except for one thing that's making it un-usable for me: I can not figure out how to append to the top (beginning) of a file.


1) read the file content and put it into a variable ($old)
2) overwrite the file with $new.$old
old jasonb
Moderator
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by old jasonb »

nilson wrote:Simple enough, but the issue here is that I would like to have one include statement on the page that the blog is to be displayed on.

Code: Select all

for ($loop; $loop <= $postcounter; $loop++)
  {
     $postfile = "/filepath/" . $postcounter . ".txt";
     include $postfile;
  }

That's assuming that you give each of the files a .txt extension.
User avatar
nilson
Posts: 4100
Joined: February 15th, 2003, 11:55 pm
Location: Tuscaloosa, Alabama
Contact:

Post by nilson »

For tonigt, I'll go with the hacked-up append to front of file solution, but tomorrow, I'll probably switch it over to the cleaner one.

Is this code correct:

Code: Select all

if($pass = $setpass && $title && $message)
{
$filename = 'posts.txt';
$open = fopen ($filename, "r");
$oldcontent=fread($open, filesize($filename));
$dateid=date("Fd");
$fulldate=date("l, F d, Y");
$newcontent="<div class=\"header\"><span class=\"title\">$title</span><span class=\"date\" id=\"$dateid\">$fulldate</span></div>\n<p>$message</p>\n";
fwrite(fopen($filename, "w"), $newcontent.$oldcontent)
}
User avatar
nilson
Posts: 4100
Joined: February 15th, 2003, 11:55 pm
Location: Tuscaloosa, Alabama
Contact:

Post by nilson »

I've made a basic version of my blogger available for download: http://home.earthlink.net/~nilsoncain/files/nilsonsBlogger03.zip.
Post Reply