The purpose of the script is to search recursively through the current directory open of any files, read if the files contain a certain string and if so replace it.
However, this doesn't work.
Listing the directories and files is not a problem, but doing doing the string matching and replacing doesn't work at all.
any help would be appreciated.
//begin code
#!/usr/local/bin/php
<?
$dir = '.';
$handle = opendir($dir) or die("Couldn't open the directory");
if(is_dir($dir)) {
while(false !== ($file = readdir($handle))) {
if($file !=="." && $file !=="..") {
if (is_dir($file) ) {
$newDir = $file;
echo "Directory is: ".$newDir."\n";
echo "Files within are: \n";
$newHandle = opendir($newDir) or die("Failed opening Subdirectory: ".$newDir);
while(false !== ($newFile = readdir($newHandle))) {
if($newFile !== '.' && $newFile !=='..') { echo $newFile."\n"; }
if($newFile !="." && $newFile != ".." ) {
$fd = @fopen($newFile, 'w') or die("Error opening the file");
$contents = @fread($fd, filesize($newFile)) or die("Error Reading the file\n $newFile\n");
if(preg_match('/mail3/', 'mail', $contents)) {
$contents = preg_replace('/mail3/', 'mail', $contents);
echo "Succesully updated file\n\n";
fclose($fd) or die("Couldn't close the file");
}
}
}
}
}
}
}
closedir($handle);
?>