Hi everyone
After scouring through the Forums here I have put together some code to detect if there is a particular line in a file and if there isn’t this line to open the file and write the line into it.
I have it kind of working but was wondering if
1) There is a better more practical way of coding it
2) Get the line entered in a particular part of the file.
The file that the code has to search will be something similar to this:
<?PHP
/* Some Kind of Info Here **************** */
/* */
/* */
/* */
/* *************************************** */
/* There may be other code *************** */
inclue_once(“somefile1.php”);
?>
I need my code to open that file and search for this a line of code similar to:
$searchfor = ‘include_once(“somefile2.php”);’
If it finds the code in the page then the script returns Code Found and does nothing else, but if it not found in the file then I want to enter 2 lines into the file, just before the ?>, When I do it with the code below it enters one line of the code (the second one) and deletes/replaces the ?>
This is the code I am using:
$i = 0;
$Include = "Not Found";
$file = 'includes/my_header.php';
$searchfor = 'include_once("somefile2.php");';
$lines = file($file);
while ($lines[$i] != "")
{
echo "$lines[$i]";
if (eregi("$searchfor", $lines[$i])) $Include = "File Found";
$i++;
}
if ($Include == "Not Found")
{
$i = $i - 1;
$lines[$i] = "/* Some Note Here *********** */");";
$lines[$i] = "include_once(\"somefile2.php\");";
$data = implode($lines,"\n");
$fp = fopen($file,"w");
fwrite($fp,$data);
fclose($fp);
}
Another thing I noticed, when I open the file after this code has inserted the line all the lines are double spaced, any idea why its doing that.
On another subject is there any PHP code that can change chmod settings?
Thanks very much for any help you can give.