Solved

File Appending... Backwards like.

Posted on 2001-07-18
7
249 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: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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Functions in phpMyAdmin 8 26
Logic behind "best rated" calculation 11 31
Read a remote csv file from a https URL 8 49
Log in through ID 5 19
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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…

828 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