Solved

File Appending... Backwards like.

Posted on 2001-07-18
7
217 Views
Last Modified: 2012-05-05
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");
?>
0
Comment
Question by:Psylord
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 6293617
<?
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.
0
 
LVL 8

Expert Comment

by:us111
ID: 6293680
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);
?>
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6293697
Eek. Sorry.

And so close too.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Accepted Solution

by:
us111 earned 100 total points
ID: 6293713
<?
    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);

    $content  = join("", 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".$content);

    fclose($fp);
?>

I've just tested it works, and it's better, less code
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6293747
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!
0
 
LVL 8

Expert Comment

by:us111
ID: 6293780
yes but the space will  be \r\n
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 6293795
Yep.

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now