Psylord
asked on
File Appending... Backwards like.
Hello,
I'm writing a news system and it's moving along quite well. I'm at the point now where I'm writing items to a flat file (please no database suggestions) and my only gripe about what it does is that it write newest entries to the bottom of the file. I would like someone to simply modify the code below to write to the top of the file, and have existing lines follow afterwards. NOTE: It is important that previous items keep their same order. Please use efficient code, and thanks in advance.
THE CODE:
<?
include("oauth/authorize.p hp");
?>
<?
$template = implode('',file('news.tmpl '));
$template = str_replace ("{InsertNews}", $newscontent, $template);
$template = str_replace ("{InsertTitle}", $newstitle, $template);
$template = str_replace ("{InsertDateTime}", $datetime, $template);
$template = str_replace ("{InsertName}", $name, $template);
$fp = fopen ("C:/Inetpub/Omniformity.c om/oNews/n ews.txt", "a");
fwrite($fp,"".stripslashes ($template )."<br><br >\r\n");
?>
I'm writing a news system and it's moving along quite well. I'm at the point now where I'm writing items to a flat file (please no database suggestions) and my only gripe about what it does is that it write newest entries to the bottom of the file. I would like someone to simply modify the code below to write to the top of the file, and have existing lines follow afterwards. NOTE: It is important that previous items keep their same order. Please use efficient code, and thanks in advance.
THE CODE:
<?
include("oauth/authorize.p
?>
<?
$template = implode('',file('news.tmpl
$template = str_replace ("{InsertNews}", $newscontent, $template);
$template = str_replace ("{InsertTitle}", $newstitle, $template);
$template = str_replace ("{InsertDateTime}", $datetime, $template);
$template = str_replace ("{InsertName}", $name, $template);
$fp = fopen ("C:/Inetpub/Omniformity.c
fwrite($fp,"".stripslashes
?>
Same as RQuadling but correction, $origfile is an array
<?
include("oauth/authorize.p hp");
?>
<?
$template = implode('',file('news.tmpl '));
$template = str_replace ("{InsertNews}", $newscontent, $template);
$template = str_replace ("{InsertTitle}", $newstitle, $template);
$template = str_replace ("{InsertDateTime}", $datetime, $template);
$template = str_replace ("{InsertName}", $name, $template);
$origfile = file("C:/Inetpub/Omniformi ty.com/oNe ws/news.tx t");
$fp = fopen ("C:/Inetpub/Omniformity.c om/oNews/n ews.txt", "w");
fwrite($fp,"".stripslashes ($template )."<br><br >\r\n");
for($i=0;$i<count($origfil e);$i++)
fwrite($fp,$origfile[$i]);
fclose($fp);
?>
<?
include("oauth/authorize.p
?>
<?
$template = implode('',file('news.tmpl
$template = str_replace ("{InsertNews}", $newscontent, $template);
$template = str_replace ("{InsertTitle}", $newstitle, $template);
$template = str_replace ("{InsertDateTime}", $datetime, $template);
$template = str_replace ("{InsertName}", $name, $template);
$origfile = file("C:/Inetpub/Omniformi
$fp = fopen ("C:/Inetpub/Omniformity.c
fwrite($fp,"".stripslashes
for($i=0;$i<count($origfil
fwrite($fp,$origfile[$i]);
fclose($fp);
?>
Eek. Sorry.
And so close too.
And so close too.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's a LOT better!
The file command will read the lines into an array and keep the newline stuff (I wondered about that, RTFM).
The join will create a single variable of the array of the lines with no extra space between them!
Clever!
The file command will read the lines into an array and keep the newline stuff (I wondered about that, RTFM).
The join will create a single variable of the array of the lines with no extra space between them!
Clever!
yes but the space will be \r\n
Yep.
include("oauth/authorize.p
?>
<?
$template = implode('',file('news.tmpl
$template = str_replace ("{InsertNews}", $newscontent, $template);
$template = str_replace ("{InsertTitle}", $newstitle, $template);
$template = str_replace ("{InsertDateTime}", $datetime, $template);
$template = str_replace ("{InsertName}", $name, $template);
// Read the original file into a simple array.
$origfile = file("C:/Inetpub/Omniformi
// Create a new file.
$fp = fopen ("C:/Inetpub/Omniformity.c
// Write the new line to the new file.
fwrite($fp,"".stripslashes
// Write the original lines to the new file.
fwrite($fp,$origfile);
// Close the new file.
fclose($fp);
?>
Regards,
Richard Quadling.