FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free 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.
Related topics
about file limit
can i upload a rar file to ftp?
File upload with PHP, Build an upload database for your site
File Size Limit
Windows Tips&tricks!
US Supreme Court rules against file swapping
Capturing multiple USB cams to an AVI or MPEG file.
SLOW UPLOADING AN SQL FILE
File size
UPLOAD STILL NOT WORKING?!
users online, using php and text file
Executing a .sql file through PHP
need a file that's in Baldur's gate's 5.cd
Direct Admin editor appending text to XML and XSLT files
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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