Php batch replacement program instance code. The code is as follows :? Php *************************************** * ********************************** batch-replace, v1.1 *************************************** * ************************ the code is as follows:
/*************************************** ************************************
Batch-replace, v1.1
**************************************** ***********************************
File: batch-replace_utf8.php
Functionality: This program can scan all the files in the specified directory to replace the content. It can be used to delete Trojans in batches and to update some content on the page in batches.
This procedure applies to modifying the page of the UTF-8.
/*************************************** ************************************
*
* This program is free software; you can redistribute it and/or modify
* It under the terms of the GNU Lesser General Public License as published
* The Free Software Foundation; either version 2 of the License, or
* (At your option) any later version.
*
**************************************** ***********************************/
Set_time_limit (3600 );
If ($ _ POST ['submit '] = 'start to execute operation '){
$ Dir = $ _ POST ['searchpath'];
$ Shortname = $ _ POST ['shortname '];
$ Isall = $ _ POST ['isall'];
$ Isreg = $ _ POST ['isreg '];
If (! Get_magic_quotes_gpc ()){
$ Sstr = $ _ POST ['sstr'];
$ Rpstr = $ _ POST ['rpstr'];
} Else {
$ Sstr = stripslashes ($ _ POST ['sstr']);
$ Rpstr = stripslashes ($ _ POST ['rpstr']);
}
// Analyze shortname
$ Arrext = explode ("|", $ shortname );
If (! Is_dir ($ dir) return;
If ($ sstr = '') return;
// Remove the ending slash (/)
If (substr ($ dir,-1) = '/') $ dir = substr ($ dir, 0, strrpos ($ dir ,"/"));
// List all directories
If ($ isall = 1 ){
Hx_dirtree ($ dir );
} Else {
Hx_dealdir ($ dir );
}
Exit ();
}
Function hx_dirtree ($ path = "."){
Global $ sstr, $ rpstr, $ isreg, $ arrext;
$ D = dir ($ path );
While (false! ==( $ V = $ d-> read ())){
If ($ v = "." | $ v = "..") continue;
$ File = $ d-> path. "/". $ v;
If (is_dir ($ file )){
Echo"
$ V
"; Hx_dirtree ($ file );
} Else {
$ Ext = substr (strrchr ($ v, "."), 1 );
If (in_array ($ ext, $ arrext )){
Echo"
$ File ";
$ Body = file_get_contents ($ file );
If ($ isreg = 1 ){
$ Body2 = preg_replace ($ sstr, $ rpstr, $ body );
} Else {
$ Body2 = str_replace ($ sstr, $ rpstr, $ body );
}
If ($ body! = $ Body2 & $ body2! = ''){
Tofile ($ file, $ body2 );
Echo 'OK ';
} Else {
Echo 'no ';
}
Echo'';
}
}
}
$ D-> close ();
}
Function hx_dealdir ($ dir ){
Global $ sstr, $ rpstr, $ isreg, $ arrext;
If ($ dh = opendir ($ dir )){
While (false! ==( $ File = readdir ($ dh ))){
If (filetype ($ dir. '/'. $ file) = 'file '){
$ Ext = substr (strrchr ($ file, "."), 1 );
If (in_array ($ ext, $ arrext )){
Echo"
$ File ";
$ Body = file_get_contents ($ dir. '/'. $ file );
If ($ isreg = 1 ){
$ Body2 = preg_replace ($ sstr, $ rpstr, $ body );
} Else {
$ Body2 = str_replace ($ sstr, $ rpstr, $ body );
}
If ($ body! = $ Body2 & $ body2! = ''){
Tofile ($ dir. '/'. $ file, $ body2 );
Echo 'OK ';
} Else {
Echo 'no ';
}
Echo'';
}
}
}
Closedir ($ dh );
}
}
// Write the function of the file generation process
Function tofile ($ file_name, $ file_content ){
If (is_file ($ file_name )){
@ Unlink ($ file_name );
}
$ Handle = fopen ($ file_name, "w ");
If (! Is_writable ($ file_name )){
Return false;
}
If (! Fwrite ($ handle, $ file_content )){
Return false;
}
Fclose ($ handle); // Close the pointer
Return $ file_name;
}
?>
Batch replace program | delete Trojans in batches _ www.itlearner.com
Bulk replacement program (UTF-8 edition)
This program can scan all the files in the specified directoryContent replacement. It can be used to delete Trojans in batches and to update some content on the page in batches.
When the number of files is very large, this operation occupies server resources. make sure that the script time-out period can be changed. Otherwise, the operation may fail.