Write to the top of a flat 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

User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Write to the top of a flat file

Post by dull1554 »

i wrote a news script it has a form that i enter the new news into, then my php script parses the form and writes it to a text file, it writes it to the bottom of the file, i'd like it so it writes it to the top so the newest news is posted at the top of the file.

files used:
news_admin.php
add_news.php
clear_news.php
news.php
news.txt

CODD:

news_admin.php

Code: Select all

<html>
<head>
<title>
</title>
</head>
<body>
<?php include "news.php" ?>
<hr width='85%'>
<center><table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'>
   <tr><td width='100%' border='1' bgcolor='whitesmoke'>New News
   </td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'><form action='add_news.php' method='post'>
Subject: <input type="text" name="subject">
<br>
News: <br> <textarea name="news" rows='5' cols='30' wrap="soft">Enter your News here!</textarea>
<input type='submit' value='submit'></form><form action='clear_news.php' method='post'>
Clear News: <input type='submit' value='clear'>
</form></td></tr></table>   </center>




</body>
<!--(C) Dull1554 -->
</html>
add_news.php

Code: Select all

<?php

if((!isset($_POST['subject'])) or (!isset($_POST['news'])))
{
     echo 'You must enter <b>ALL</b> fields before you can continue!';
     exit;
}
$subject = $_POST['subject'];
$news = $_POST['news'];
$time = date("D d of F, h:i a");

$fp = fopen("news.txt", "a+");
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>";
fwrite($fp, $insertcode);
fclose($fp);

?>
clear_news.php

Code: Select all

<?php

    $fp = fopen("news.txt", "w+");
    $insertcode = "";
    fwrite($fp, $insertcode);
    fclose($fp);

?>
news.php

Code: Select all

<html>
<head>
<link rel="stylesheet" type="text/css" href="../styles.css"/>
<script type="text/javascript" src="../fader.js"></script>
<title>
</title>
</head>
<body>
 <center><img src=../images/header.png width=100%></center>
<h3>News</h3>
<p>
<?php include "news.txt" ?> 
</body>
<!--(C) Dull1554 -->
</html>
and news.txt is a blank file
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

use r+ instead of w+


and check the manual next time ;)
http://us2.php.net/manual/en/function.fopen.php
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Or try this on your add news script... this is how I'd do it.

Code: Select all

<?php


if((!isset($_POST['subject'])) or (!isset($_POST['news']))) 
{ 
     echo 'You must enter <b>ALL</b> fields before you can continue!'; 
     exit; 
} 
$subject = $_POST['subject']; 
$news = $_POST['news']; 
$time = date("D d of F, h:i a"); 

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

$fp = fopen("news.txt", "w+"); 
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>$oldnews"; 
fwrite($fp, $insertcode); 
fclose($fp); 

?>
see what I did.... I added $oldnews = file("news.txt"); to get the old news from the textfile. Then opened the file with w+ which empties the file out and makes it writable. It's ok for it to empty it out cause we already have the data in the $oldnews variable. Then just stick the $oldnews on the end of your new news..... voila

btw lilpunk... he was right to use w+ for emptying the file out.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

php.net wrote:'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
php.net wrote:'r+' Open for reading and writing; place the file pointer at the beginning of the file.
r+ would do the same thing your code does, but without having to read the file into a variable first.. it just adds the new stuff to the top, like he wanted
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Lol yeah yer right... but you mean r+ in stead of a+ in his scripts.... little confusion... never hurt anyone hehe
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

where did i say a+?
lilpunkskater wrote:use r+ instead of w+
im so confused :(

lol
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

LiLpunkSkateR wrote:use r+ instead of w+


and check the manual next time ;)
http://us2.php.net/manual/en/function.fopen.php
Now look at his code in clear_news which is the only place he uses w+ and he shouldn't use r+

he uses a+ in add_news where he should using your solution use r+

Now stop it... it's almost 5am over here and I am getting really fuzzy ;)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

thanks everyone

Post by dull1554 »

you guys are great, i really appreciate the help, i chose to Ic's soultion, i've had problems with r=, but thanks to everyone
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

lc wrote:
LiLpunkSkateR wrote:use r+ instead of w+


and check the manual next time ;)
http://us2.php.net/manual/en/function.fopen.php
Now look at his code in clear_news which is the only place he uses w+ and he shouldn't use r+

he uses a+ in add_news where he should using your solution use r+

Now stop it... it's almost 5am over here and I am getting really fuzzy ;)
lol alright, i totally looked over that :P
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Dull the reason I do it the way I do in stead of with the more obvious r+ is that I write all my options into a single write statement.

So the way I format working with textfiles is:

Get all data in an array.

then do to it whatever needs doing (adding, deleting, reordering, editing)

then write it all back.

it's not always the quickest way, but I find it easier for me to figure out what I'm doing.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

honnestly this still doen not do what i want it too, i want it to insert the new code into the top of the txt file so the newest stuff is displayed at the yop of the file, r+ just inserts it to the top but it overwrites whatever was underneath
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Hmm

Post by William »

Im making a news poster to. And i have problems. Im trying to do it in Arrays now. So they can deleate 1 post at a time and not all of them and so they cant see teh posts unless they go to the page. IM me on AIM: The Hidden Viper. And ill try tp help as soon as i can get this done or MSN: wdd82689@mchsi.com or yahoo: the_hidden_viper or ICQ: 232372049. Thats about all of the instant messengers i have dont have IRC sorry. There all on so. Im me when ready cya
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

use 'w'
mode Description
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

You can't write to the begining of a file.

The only really possible solution is as lc[/lc] said:

Code: Select all

<?php
if((!isset($_POST['subject'])) or (!isset($_POST['news']))) 
{ 
     echo 'You must enter <b>ALL</b> fields before you can continue!'; 
     exit; 
} 
$subject = $_POST['subject']; 
$news = $_POST['news']; 
$time = date("D d of F, h:i a"); 

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

$fp = fopen("news.txt", "w");
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>$oldnews"; 

 
fwrite($fp, $insertcode); 
fwrite($fp, join('',$oldnews);
fclose($fp); 
?>
The only real difference between the above code and lc's code is that there is no point in using w+ as you have no call to open it for reading, just writing... Also added the line to write the old data back to the file after the new line.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

reply

Post by dull1554 »

Code: Select all

<?php
if((!isset($_POST['subject'])) or (!isset($_POST['news'])))
{
     echo 'You must enter <b>ALL</b> fields before you can continue!';
     exit;
}
$subject = $_POST['subject'];
$news = $_POST['news'];
$time = date("D d of F, h:i a");

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

$fp = fopen("news.txt", "w");
$insertcode = "<table width='100%' border='0' cellspacing='1' cellpadding='2' bgcolor='#dcdcdc' align='center'><tr><td width='100%' border='1' bgcolor='whitesmoke'>".$subject." - ".$time."</td></tr><tr><td width='100%' border='1' bgcolor='#ffffff'>".$news."</td></tr></table><br>".$oldnews;// old news can not be in ""s


fwrite($fp, $insertcode);
fwrite($fp, join('',$oldnews);
fclose($fp);
?> 

?>
i also get a parse error on line 19 saying unexpected ;
Post Reply