FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Appending file

 


coreymanshack
I'm writing to a file with php, and I was wondering how do I append to the front of the file rather than append to the end of the file?

Better yet, how do I append after a certain string in the file?
hexkid
You don't. You read one file and write another. At the end of the script, you can erase the input file and rename the output file.

Add "extra line" after a line with "foobar":
Code:
<?php
$input_file = fopen("/tmp/inputfile.txt", "rb");
$output_file = fopen("/tmp/outputfile.txt", "wb");
if ($input_file && $output_file) {
  while (!feof($input_file)) {
    $buffer = fgets($input_file, 4096);
    fputs($output_file, $buffer);
    if (trim($buffer) = "foobar") {
      fputs($output_file, "extra line\n");
    }
  }
  fclose($input_file);
  fclose($output_file);
  if (true) {
    unlink("/tmp/inputfile.txt");
    rename("/tmp/outputfile.txt", "/tmp/inputfile.txt");
  }
}
?>
dotapinoy3480
hey why dont you use MySQL? its much more easier to manipulate data....


use this site... its great... i done loads of things there...

www.w3schools.com

Razz
coreymanshack
dotapinoy3480 wrote:
hey why dont you use MySQL? its much more easier to manipulate data....


use this site... its great... i done loads of things there...

www.w3schools.com

Razz


What I want to do is not practial for mysql, I'm storing a whole html page.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.