?
Solved

File Appending... Backwards like.

Posted on 2001-07-18
7
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
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:Richard Quadling
ID: 6293697
Eek. Sorry.

And so close too.
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 8

Accepted Solution

by:
us111 earned 400 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:Richard Quadling
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:Richard Quadling
ID: 6293795
Yep.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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