Prepending to a File


You want to write content to the beginning of a file.


Use the File::prepend() method.

$bytesWritten = File::prepend($filename, $content);
if ($bytesWritten === false)
    die("Couldn't write to the file.");

This will add the content to the beginning of existing files. If the file doesn't exist then the entire contents will be $content.


Watch the return value.

If there's a problem writing to the file false is returned.

comments powered by Disqus