Hi,
I wrote this to insert some paths and file ID's into pre-written templates. Note that in this case you need to know WHERE to insert the data, and there needs to be space. Else, you could loop through the file, read each line, write it to a temp file, until you have the line that you need to insert, write the new info, and continue reading/writing the file. After you have done the whole file, you move the tmp file in the place of the old file.
Hope it helps:
$basefile = file($basefilelocation);
$newfname = str_replace("/","\\", $newfname);
$fp = fopen("$newfname", "w"); // create / open the new file
foreach($basefile as $line_num => $line)
{
if($line_num == 3)
{
$writecontent = '$pageid = '.$page_id.";
require(\"".$prefix."includes/websiteconfig.php\");
";
if(!fwrite($fp, $writecontent."\n"))
{
$body.= "Cannot write to file ($newfname)";
exit();
}
}
if (!fwrite($fp, $line.""))
{
$body.= "Cannot write to file ($newfname)";
exit();
}
}
}