Jump to content


Appending to file on top, not bottom

  • Please log in to reply
2 replies to this topic

#1 solarisuser

  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 19 April 2006 - 05:30 PM

Hello, I use fputs to write to a file, but I can only find documentation stating this function writes to the bottom of the file. I'd like to add text to the beginning of a file. Anyone out there have some code to do such a thing?


#2 ober

  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 19 April 2006 - 06:52 PM

You will have to read the file into a string, write your new output to the file and then write your old string back to the end of the file. There's no function to do this otherwise.

Info: PHP Manual

#3 litebearer

  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 20 April 2006 - 02:49 AM

try this

function write_beg($filename, $data){
    //Imports old data
    $handle = f open($filename, "r");
    $old_content = f read($handle, filesize ($filename));
    f close($handle);

    //Sets up new data
    $final_content = $data.$old_content;

    //Writes new data
    $handle2 = f open($filename, "w");
    $finalwrite = f write($handle2, $final_content);
    f close($handle2);

everywhere there is a f with a space after it, remove the space.


all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users