Solved

File Appending... Backwards like.

Posted on 2001-07-18
7
259 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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: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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

690 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