EXEC ("/bin/grep-r ' $oldword ' $rootpath", $results, $errorCode);
if ($errorCode) {
if ($errorCode = = 1) {
echo "Possibly no files were found with $oldword in them
\ n ";
}
echo "OS Error: $errorCode
\ n ";
echo "Check ' man errno ' and count down
\ n ";
echo "usually paths/permissions
\ n ";
}
while (list (, $path) = each ($results)) {
$parts = Explode (': ', $path);
$path = $parts [0];
$fp = fopen ($path, ' r ') or print ("Cannot read $path
\ n ");
if ($fp) {
$data = Fread ($fp, FileSize ($path));
Fclose ($FP);
$newdata = Str_replace ($oldword, $newword, $data);
$fp = fopen ($path, ' W ') or print ("Cannot write $path
\ n ");
if ($fp) {
Fwrite ($fp, $newdata);
Fclose ($FP);
Echo $path, "
\ n ";
}
}
}
?>
Example
Http://yourserver.com/globalreplace.php?oldword=test&newword=text&rootpath=/path/to/dir
http://www.bkjia.com/PHPjc/315906.html www.bkjia.com true http://www.bkjia.com/PHPjc/315906.html techarticle PHP exec (/bin/grep-r ' $oldword ' $rootpath, $results, $errorCode); if ($errorCode) {if ($errorCode = = 1) {echo Possib Ly no files were found with $oldword in thembr\n; } echo OS Err ...