当前位置:首页 > PHP教程 > php高级应用 > 列表

php批量替换程序实例代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-27 14:54:47 浏览: 评论:0 

本程序可以实现批量替换目录中所有文件中的内容或用于打量给挂了木马文件批量替换与更新了,希望文章对各位同学有所帮助.

php 批量替换程序实例代码如下:

  1. <?php 
  2. /*************************************************************************** 
  3.                              batch-replace, v1.1 
  4.  *************************************************************************** 
  5.     file:                batch-replace_utf8.php 
  6.     functionality:       本程序可以扫描指定目录的所有文件,进行内容替换。可用于被批量挂马的删除以及批量更新页面某些内容。  
  7.                          本程序适用于对UTF-8的页面进行修改。 
  8.                           
  9.  
  10.  
  11. /*************************************************************************** 
  12.  * 
  13.  *   This program is free software; you can redistribute it and/or modify 
  14.  *   it under the terms of the GNU Lesser General Public License as published by 
  15.  *   the Free Software Foundation; either version 2 of the License, or 
  16.  *   (at your option) any later version. 
  17.  * 
  18.  ***************************************************************************/ 
  19.  
  20. set_time_limit(3600); 
  21.  
  22.  
  23. if($_POST['Submit']=='开始执行操作'){ 
  24.   $dir = $_POST['searchpath']; 
  25.   $shortname = $_POST['shortname']; 
  26.   $isall = $_POST['isall']; 
  27.   $isreg = $_POST['isreg']; 
  28.    
  29. if (!get_magic_quotes_gpc()) { 
  30.   $sstr = $_POST['sstr']; 
  31.   $rpstr = $_POST['rpstr']; 
  32. } else { 
  33.   $sstr = stripslashes($_POST['sstr']); 
  34.   $rpstr = stripslashes($_POST['rpstr']); 
  35. }     
  36.  
  37.  
  38.   //分析shortname 
  39.   $arrext = explode ("|",$shortname); 
  40.  
  41.  
  42.   if (!is_dir($dir)) return; 
  43.   if ($sstr == '') return; 
  44.  
  45.   //把末尾的/去掉 
  46.   if(substr($dir,-1)=='/') $dir = substr($dir,0,strrpos($dir,"/")); 
  47.  
  48.   //罗列所有目录 
  49.   if ($isall == 1){ 
  50.     hx_dirtree($dir); 
  51.   }else{ 
  52.     hx_dealdir($dir); 
  53.  
  54.   } 
  55.  
  56. exit(); 
  57. } 
  58.  
  59.  
  60. function hx_dirtree($path="."){ 
  61.   global $sstr,$rpstr,$isreg,$arrext; 
  62.  
  63.  
  64.   $d = dir($path); 
  65.   while(false !== ($v = $d->read())) { 
  66.     if($v == "." || $v == "..") continue; 
  67.     $file = $d->path."/".$v; 
  68.     if(is_dir($file)) { 
  69.       echo "<p>$v</p>"; hx_dirtree($file); 
  70.     }else{ 
  71.         $ext=substr(strrchr($v,"."), 1); 
  72.         if( in_array($ext , $arrext) ){ 
  73.           echo "<li>$file "; 
  74.           $body = file_get_contents($file); 
  75.           if($isreg == 1){ 
  76.           $body2 = preg_replace($sstr, $rpstr, $body); 
  77.           }else{ 
  78.           $body2 = str_replace($sstr, $rpstr, $body); 
  79.           } 
  80.           if($body != $body2 && $body2 != ''){ 
  81.             tofile($file,$body2); 
  82.             echo ' OK'; 
  83.           }else{ 
  84.             echo ' NO'; 
  85.           } 
  86.           echo '</li>'; 
  87.         } 
  88.     } 
  89.   } 
  90.   $d->close(); 
  91. } 
  92.  
  93. function hx_dealdir($dir){ 
  94.   global $sstr,$rpstr,$isreg,$arrext; 
  95.     if ($dh = opendir($dir)) { 
  96.     while (false !== ($file = readdir($dh))) { 
  97.       if(filetype($dir.'/'.$file)=='file'){ 
  98.  
  99.         $ext=substr(strrchr($file,"."), 1); 
  100.         if( in_array($ext , $arrext) ){ 
  101.  
  102.           echo "<li>$file "; 
  103.           $body = file_get_contents($dir.'/'.$file);         
  104.           if($isreg == 1){ 
  105.           $body2 = preg_replace($sstr, $rpstr, $body); 
  106.           }else{ 
  107.           $body2 = str_replace($sstr, $rpstr, $body); 
  108.           } 
  109.           if($body != $body2 && $body2 != ''){             
  110.             tofile($dir.'/'.$file,$body2); 
  111.             echo ' OK'; 
  112.           }else{ 
  113.             echo ' NO'; 
  114.           } 
  115.           echo '</li>'; 
  116.         } 
  117.       } 
  118.     } 
  119.     closedir($dh); 
  120.     } 
  121.  
  122. } 
  123. //把生成文件的过程写出函数 
  124. function tofile($file_name,$file_content){ 
  125.  if (is_file ($file_name)){ 
  126.   @unlink ($file_name); 
  127.  } 
  128.   $handle = fopen ($file_name,"w"); 
  129.   if (!is_writable ($file_name)){ 
  130.     return false; 
  131.   } 
  132.   if (!fwrite ($handle,$file_content)){ 
  133.     return false; 
  134.   } 
  135.   fclose ($handle); //关闭指针 
  136.   return $file_name; 
  137. } 
  138. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  139. <html xmlns="http://www.w3.org/1999/xhtml"> 
  140. <head> 
  141. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  142. <title>批量替换程序|木马批量删除_www.itlearner.com</title> 
  143. <style type="text/css"> 
  144. body{background:#FFFFFF;color:#000;font-size:12px;} 
  145. #top{text-align:center;} 
  146. h1,p,form{margin:0;padding:0;} 
  147. h1{font-size;14px;} 
  148. </style> 
  149. </head> 
  150. <body> 
  151.   <div id="top"> 
  152. <h1>批量替换程序(UTF-8版)</h1> 
  153. <div>本程序可以扫描指定目录的所有文件,进行<strong>内容替换</strong>。可用于被批量挂马的删除以及批量更新页面某些内容。<br/> 
  154. 在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div> 
  155.   </div> 
  156.  
  157.  
  158. <form action="<?=$_SERVER['SCRIPT_NAME']?>" name="form1" target="stafrm" method="post"> 
  159. <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666"> 
  160.   <tr> 
  161.     <td width="10%" bgcolor="#FFFFFF"><strong>&nbsp;起始根路径:</strong></td> 
  162.     <td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" id="searchpath" value="./test" size="20" /> 
  163.       点表示当前目录,末尾不要加/ <input type="checkbox" name="isall" value="1" />包含此目录下所有目录</td> 
  164.   </tr> 
  165.   <tr> 
  166.     <td bgcolor="#FFFFFF"><strong>&nbsp;文件扩展名:</strong></td> 
  167.     <td bgcolor="#FFFFFF"><input name="shortname" type="text" id="shortname" size="20" value="php|htm" /> 
  168.       多个请用|隔开</td> 
  169.   </tr> 
  170.   <tr id="rpct"> 
  171.     <td height="64" colspan="2" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="1" cellpadding="1"> 
  172.       <tr bgcolor="#EDFCE2"> 
  173.         <td colspan="4"><strong>内容替换选项:</strong> <input type="checkbox" name="isreg" value="1" />使用正则表达式</td> 
  174.       </tr> 
  175.       <tr> 
  176.         <td colspan="4">替换内容类默认使用字符串替换,也可以使用正则表达式(需勾选)。"替换为"不填写的话,就表示删除"替换内容"。</td> 
  177.       </tr> 
  178.       <tr> 
  179.         <td width="10%">&nbsp;替换内容:</td> 
  180.         <td width="36%"><textarea name="sstr" id="sstr" style="width:90%;height:45px"></textarea></td> 
  181.         <td width="10%">替 换 为:</td> 
  182.         <td><textarea name="rpstr" id="rpstr" style="width:90%;height:45px"></textarea></td> 
  183.       </tr> 
  184.     </table></td> 
  185.   </tr> 
  186.   <tr> 
  187.     <td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input type="submit" name="Submit" value="开始执行操作" class="inputbut" /></td>//开源代码phpfensi.com 
  188.   </tr> 
  189. </table> 
  190.   </form> 
  191. <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666"> 
  192.   <tr bgcolor="#FFFFFF"> 
  193.     <td id="mtd"> 
  194.      <div id='mdv' style='width:100%;height:100;'> 
  195.         <iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe> 
  196.       </div> 
  197.       <script type="text/javascript"> 
  198.      document.all.mdv.style.pixelHeight = screen.height - 450; 
  199.      </script>    </td> 
  200.   </tr> 
  201. </table> 
  202.  
  203. </body> 
  204. </html> 

Tags: php批量替换 php替换程序

分享到: