Jump to content

Archived

This topic is now archived and is closed to further replies.

solarisuser

Appending to file on top, not bottom

Recommended Posts

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?

Thanks

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
try this

[code]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);
} [/code]

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

Lite...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.