hi
I am trying to add a few line of code using fopen
It works and it adds the lines but I have multiple occurrence of this line and I would like to all be amended.
$target_line='$second = (int)substr($raw_date, 17, 2);';
$lines_to_add= '$raw_datetime = translate_from_gregorian($raw_datetime);'. PHP_EOL.
'$year = (int)substr($raw_datetime, 0, 4);'. PHP_EOL.
'$month = (int)substr($raw_datetime, 5, 2);'. PHP_EOL.
'$day = (int)substr($raw_datetime, 8, 2);'. PHP_EOL;
$config ='includes/functions/general.php';
$file=fopen($config,"r+") or exit("Unable to open file!");
$insertPos=0; // variable for saving //Users position
while (!feof($file)) {
$line=fgets($file);
if (strpos($line,$target_line)!==false) {
$insertPos=ftell($file); // ftell will tell the position where the pointer moved, here is the new line after //Users.
$newline = $lines_to_add;
} else {
$newline.=$line; // append existing data with new data of user
}
}
fseek($file,$insertPos); // move pointer to the file position where we saved above
fwrite($file, $newline);
fclose($file);