I have some websites and I want to add a new code just after <?php I wrote this code but not works:
<?php
error_reporting(0);
$dir = __DIR__;
$index = $dir.'/index.php';
if (is_file($index)) {
$content = file_get_contents($index);
if (strpos($content, 'validator') === false) {
str_replace('<?php', '<?php require_once \'path/validator.php\';', $content);
//Write the index:
$write = fopen($index,"w");
fwrite($write,$content);
fclose($write);
}
//Check again:
$content = file_get_contents($index);
if (strpos($content, 'validator') === true) {
echo "Line added successfuly";
unlink($dir.'/install.php');
} else {
echo "Line not added";
}
}
?>
How can I do this? Thanks...
strpos()
never returns true
. It either returns a numeric index or false
.
So change the second check to
if (strpos($content, 'validator') !== false)
By the way, you can use file_put_contents()
to write the file in one step, just as you use file_get_contents()
to read it.