<html>
<head>
</head>
<body>
<?php
//john pfeiffer 18apr08 Will insert text into the middle of a file BUT create date will equal modified date
//r=readonly, r+=(r+w), w=overwrite-new, w+-overwrite&read-new, a=append(write), a+=append(r+w)
$filename = "testfile.txt"; //contents should be 3 lines, test123, test456, test789
if(file_exists($filename)) //if the file actually exists
{ //the old way of getting contents $buffer = implode("", file($filename));
$fp = fopen($filename, 'r'); //open in read only mode
$buffer = file_get_contents( $filename ); //put all the file contents into a string
fclose($fp); //close the file pointer
}
$fp = fopen($filename, 'w'); //open the file again (overwrite as blank)
if (is_writable($filename)) //check to see if we have write permission
{
$oldtext = "test456"; //our target point in the file
$newtext = $oldtext . "\ntestABC"; //clever concatenated replacement text
$buffer = str_replace($oldtext, $newtext, $buffer); //handy replacement function
fwrite($fp, $buffer); //write the "improved" file back
fclose($fp);
}
else
{ fclose($fp);
clearstatcache();
echo "Cannot write to ".$filename." - Check permissions.";
}
?>
</body>
</html>