Link to home
Start Free TrialLog in
Avatar of Psylord
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.php");
?>
<?
$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.com/oNews/news.txt", "a");
fwrite($fp,"".stripslashes($template)."<br><br>\r\n");
?>
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

<?
include("oauth/authorize.php");
?>
<?
$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/Omniformity.com/oNews/news.txt");

// Create a new file.
$fp = fopen ("C:/Inetpub/Omniformity.com/oNews/news.txt", "w");

// Write the new line to the new file.
fwrite($fp,"".stripslashes($template)."<br><br>\r\n");

// Write the original lines to the new file.
fwrite($fp,$origfile);

// Close the new file.
fclose($fp);
?>

Regards,

Richard Quadling.
Same as RQuadling but correction, $origfile is an array

<?
   include("oauth/authorize.php");
?>
<?
  $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/Omniformity.com/oNews/news.txt");

  $fp = fopen ("C:/Inetpub/Omniformity.com/oNews/news.txt", "w");
   fwrite($fp,"".stripslashes($template)."<br><br>\r\n");

   for($i=0;$i<count($origfile);$i++)
        fwrite($fp,$origfile[$i]);

   fclose($fp);
?>
Eek. Sorry.

And so close too.
ASKER CERTIFIED SOLUTION
Avatar of us111
us111
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
yes but the space will  be \r\n