try something like this:
<?php
$meta_tags = "STUFF YOU JUST GENERATED IN A STRING";
$fp = fopen("./file.htm", "r");
while(!feof($fp)) {
$file .= fread($fp, 255);
}
fclose($fp);
$insertPos = strpos($file, "<head>");
if($insertPos === FALSE) {
echo "Error: No head tags found.";
} else {
$insertPos += 6; //because it really returns the place in front of head tag
$firstPart = substr($file, 0, $insertPos);
$secondPart = substr($file, $insertPos);
$fp = fopen("./file.htm", "w+");
fwrite($fp, $firstPart . "\r\n"; //print out first part of document.
fwrite($fp, $meta_tags);
fwrite($fp, $secondPart);
fclose($fp);
?>
that in effect should read the document, find where <head> is, grab the content up to that, and then grab the content after it.
write the first part up to <head> to a file, insert your meta tags and then write the rest of the document.