I want to change some code by replacing it with blank space in all php and other files in my project folder and sub folder files. I have the following code.
if ($handle = @ opendir("for testing")) {
while (($entry = readdir($handle)) ) {
if ($entry != "." && $entry != "..") {
$linecop = '/*god_mode_on*eval(test("ZXkViKSk7IA=="));*god_mode_off*/';
$homepage = file_get_contents($entry);
$string3=str_replace($linecop,'',$homepage);
$file = fopen($entry, "w") or exit("Unable to open file!");
fwrite($file, $string3);
fclose($file); //
}
}
closedir($handle);
}
But this code only works for one file. How do I change all files?
function recursive_replace( $directory, $search, $replace ) {
if ( ! is_dir( $directory ) ) return;
foreach ( glob( $directory . '/*' ) as $file ) {
if ( $file === '.' || $file === '..' ) continue;
if ( is_dir( $file ) ) recursive_replace( $file, $search, $replace );
$content = file_get_contents( $file );
$content = str_replace( $search, $replace, $content );
file_put_contents( $file, $content );
}
}
recursive_replace('/your/file/path', '/*god_mode_on*eval(test("ZXkViKSk7IA=="));*god_mode_off*/', '');
if you want to recursively search and replace, you should think of a recursive function :) Also, glob()/file_X_contents() are much nicer functions to use for file and directory needs. code not tested but is damn close to what you're looking for in any case.