最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php文件名与文件内容查找器实例

时间:2014-07-02 23:21来源:未知 作者:最模板zuimoban 点击: 次
php文件查找程序,输入一个路径确定后会遍历目录下所有的文件和文件夹,通过递归可以找到文件夹下面的每一个文件,再通过文件名和输入的关键字匹配,则可以查找到你想要的文件,对于

php文件查找程序,输入一个路径确定后会遍历目录下所有的文件和文件夹,通过递归可以找到文件夹下面的每一个文件,再通过文件名和输入的关键字匹配,则可以查找到你想要的文件,对于本地,我们可以利用windows自带的查找去进行查找,但是对于线上的话,如查找ftp空间里面文件,本程序是很有用的.

php文件查找器源码,代码如下:

  1. <html> 
  2.  <head> 
  3.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4.   <title>php版文件查找(file search)</title> 
  5.  </head> 
  6.  <body> 
  7.   <form action="" method="post"> 
  8.   <p> 文件查找(注:区分大小写)</p> 
  9.   <p>路径:<input type="text" name="path" /></p> 
  10.   <p>查找:<input type="text" name="key" /></p> 
  11.   <p><input type="submit" name="sub" value=" 开 始 " /></p> 
  12.   </form> 
  13.  </body> 
  14. </html> 
  15. <?php 
  16. /* 
  17.  * 注:区分大小写 
  18.  */ 
  19. if(!emptyempty($_POST['path'])&&!emptyempty($_POST['key'])){ 
  20.  echo "在路径 ".$_POST['path']."/ 中查找 ".$_POST['key']." 的结果为:<hr/>"; 
  21.  $file_num = $dir_num = 0; 
  22.  $r_file_num = $r_dir_num= 0; 
  23.  $findFile = $_POST['key']; 
  24.  function delDirAndFile( $dirName ){  
  25.   if ( $handle = @opendir( "$dirName" ) ) { 
  26.    while ( false !== ( $item = readdir( $handle ) ) ) {  
  27.     if ( $item != "." && $item != ".." ) {  
  28.      if ( is_dir( "$dirName/$item" ) ) {  
  29.       delDirAndFile( "$dirName/$item" ); 
  30.      } else {  
  31.       $GLOBALS['file_num']++; 
  32.       if(strstr($item,$GLOBALS['findFile'])){ 
  33.        echo " <span><b> $dirName/$item </b></span><br />n"; 
  34.        $GLOBALS['r_file_num']++; 
  35.       } 
  36.      }  
  37.     } 
  38.    } 
  39.    closedir( $handle );  
  40.    $GLOBALS['dir_num']++; 
  41.    if(strstr($dirName,$GLOBALS['findFile'])){ 
  42.     $loop = explode($GLOBALS['findFile'],$dirName); 
  43.     $countArr = count($loop)-1; 
  44.     if(emptyempty($loop[$countArr])){ 
  45.      echo " <span style='color:#297C79;'><b> $dirName </b></span><br />n"; 
  46.      $GLOBALS['r_dir_num']++; 
  47.     } 
  48.    } 
  49.   }else{ 
  50.    die("没有此路径!"); 
  51.   } 
  52.  } 
  53.  
  54.  delDirAndFile($_POST['path']); 
  55.  echo "<hr/>本次共搜索到".$file_num."个文件,文件夹".$dir_num."个<br/>"; 
  56.  echo "<hr/>符合结果的共".$r_file_num."个文件,文件夹".$r_dir_num."个<br/>"; 
  57. } 
  58. ?> 

上面只是查找文件,下面看一个查找文件中的字符是否包括我们要找的东西,自己写的一个批量查找文件内容的php程序,我是拿来扫描文件特征码的,现在我贴出代码,供大家参考,代码如下:

  1. <?php 
  2. if ($_POST ['Submit'] == '开始') { 
  3.  $total = 0; //文件总数 
  4.  $dangerous = array (); //危险文件 
  5.  $dangerous_content = $_POST ["sstr"]; 
  6.  $find_path = $_POST ["searchpath"]; 
  7.  $shortname = $_POST ["shortname"]; 
  8.  echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"; 
  9.  echo "<html>"; 
  10.  echo "<head>"; 
  11.  echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"; 
  12.  echo "</head>"; 
  13.  echo "<body>"; 
  14.  $begin_time=date("U"); 
  15.  // $dangerous_content = "小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770"; 
  16.  visitFile ( $find_path, $shortname ); 
  17.  $end_time=date("U"); 
  18.  foreach ($dangerous as $d){ 
  19.   echo $d."<br/>"; 
  20.  } 
  21.  echo "查找文件总数:" . $total." 危险文件:".count($dangerous)." 总用时".($end_time-$begin_time)."秒"; 
  22.  echo "</body>"; 
  23.  echo "</html>"; 
  24.  //if (! empty ( $dangerous )) { 
  25.   //foreach ( $dangerous as $dan ) { 
  26.  //echo "[error]" . $dan . "<br/>"; 
  27.  //} 
  28.  //} 
  29.  exit(); 
  30. } 
  31. function visitFile($path, $ext) { 
  32.  global $total; 
  33.  global $dangerous_content; 
  34.  $fdir = dir ( $path ); 
  35.  //echo "Handle: " . $d->handle . "<br>"; 
  36.  // echo "Path: " . $fdir->path . "<br>"; 
  37.  set_time_limit ( 24 * 60 * 60 ); 
  38.  
  39.  while ( ($entry = $fdir->read ()) !== false ) { 
  40.   $pathSub = $path . "\" . $entry; 
  41.   if ($entry != '.' && $entry != '..') { 
  42.    if (is_dir ( $pathSub )) { 
  43.     visitFile ( $pathSub, $ext ); 
  44.    } else { 
  45.     $exten = explode ( '.', $entry ); 
  46.     $exten = array_reverse ( $exten ); //把上面数组倒序 
  47.     //   foreach () 
  48.     $shortnames = explode ( '|', $ext ); 
  49.     foreach ( $shortnames as $sn ) { 
  50.      if (! emptyempty ( $exten ) && $sn == $exten [0]) { 
  51.       $total = $total + 1; 
  52.       //echo "开始分析文件:".$path."/".$entry . "<br>"; 
  53.       $content = file_get_contents ( $path . "/" . $entry ); //这个性能较好 
  54.       $content = strtolower ( $content ); //全部转为小写 
  55.       $dangerous_content = strtolower ( $dangerous_content ); //全部转为小写 
  56.       isExists ( $dangerous_content, $path . "/" . $entry, $content );//这个方法太耗内存了,希望有高手能解决一下 
  57.      } 
  58.     } 
  59.     //sleep(1); 
  60.    } 
  61.   } 
  62.  } 
  63.  $fdir->close (); 
  64. } 
  65. function isExists($str, $filename, $content) { 
  66.  global $dangerous; 
  67.  //sleep ( 1 ); 
  68.  set_time_limit ( 10 ); 
  69.  $arr = explode ( ',', $str ); 
  70.  $signature="特征码:"; 
  71.  if (! emptyempty ( $arr )) { 
  72.   //  $content = file_get_contents ( $filename ); //这个性能较好 
  73.   $content = strtolower ( $content ); //全部转为小写 
  74.   $error_count = 0; 
  75.   foreach ( $arr as $a ) { 
  76.    if (trim ( $a ) != "") { 
  77.     if (strpos ( $content, $a )) { 
  78.      $error_count = $error_count + 1; 
  79.      $signature.=$a." "; 
  80.     } 
  81.    } 
  82.   } 
  83.   if ($error_count > 0) { 
  84. //   $dangerous [] = $filename; 
  85.    $dangerous [] = "[error] " . $error_count . " " .$signature." " . $filename; 
  86.    //echo "[error] " . $error_count . " " .$signature." " . $filename . "<br/>"; 
  87.   }else{ 
  88.    //echo "[ok] "  . $filename . "<br/>"; 
  89.   } 
  90.  } 
  91. } 
  92. ?> 
  93. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  94. <html xmlns="http://www.w3.org/1999/xhtml"> 
  95. <head> 
  96. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  97. <title>批量查询文件</title> 
  98. <style type="text/css"> 
  99. body { 
  100.  background: #FFFFFF; 
  101.  color: #000; 
  102.  font-size: 12px; 
  103. } 
  104.  
  105. #top { 
  106.  text-align: center; 
  107. } 
  108.  
  109. h1,p,form { 
  110.  margin: 0; 
  111.  padding: 0; 
  112. } 
  113.  
  114. h1 {font-size; 14px; 
  115.  
  116. } 
  117. </style> 
  118. </head> 
  119. <body> 
  120. <div id="top"> 
  121. <h1>批量查找程序</h1> 
  122. <div>本程序可以扫描指定目录的所有文件,进行<strong>内容查找</strong>。<br /> 
  123. 在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div> 
  124. </div> 
  125.  
  126.  
  127. <form action="<?=$_SERVER ['SCRIPT_NAME']?>" name="form1" 
  128.  target="stafrm" method="post"> 
  129. <table width="95%" border="0" align="center" cellpadding="3" 
  130.  cellspacing="1" bgcolor="#666666"> 
  131.  <tr> 
  132.   <td width="10%" bgcolor="#FFFFFF"><strong>&nbsp;起始根路径:</strong></td> 
  133.   <td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" 
  134.    id="searchpath" value="D:/" size="20" /> 点表示当前目录,末尾不要加/ </td> 
  135.  </tr> 
  136.  <tr> 
  137.   <td bgcolor="#FFFFFF"><strong>&nbsp;文件扩展名:</strong></td> 
  138.   <td bgcolor="#FFFFFF"><input name="shortname" type="text" 
  139.    id="shortname" size="20" value="htm|html|shtml|php" /> 多个请用|隔开</td> 
  140.  </tr> 
  141.  <tr id="rpct"> 
  142.   <td height="64" colspan="2" bgcolor="#FFFFFF"> 
  143.   <table width="100%" border="0" cellspacing="1" cellpadding="1"> 
  144.    <tr bgcolor="#EDFCE2"> 
  145.     <td colspan="4"><strong>内容查找选项:</strong> <input type="checkbox" 
  146.      name="isreg" value="1" />使用正则表达式</td> 
  147.    </tr> 
  148.    <tr> 
  149.     <td colspan="4">查找内容类默认使用字符串查找,也可以使用正则表达式(需勾选)。"查找为"不填写的话,就表示删除"查找内容"。 
  150.      <br />com,system,exec,eval,escapeshell,cmd,passthru,base64_decode,gzuncompress 
  151.     </td> 
  152.    </tr> 
  153.    <tr> 
  154.     <td width="10%">&nbsp;查找内容:</td> 
  155.     <td width="36%" colspan="3"><textarea name="sstr" id="sstr" 
  156.      style="width: 90%; height: 45px">小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770,hx_dealdir,while(1)</textarea></td> 
  157.    </tr> 
  158.   </table> 
  159.   </td> 
  160.  </tr> 
  161.  <tr> 
  162.   <td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input 
  163.    type="submit" name="Submit" value="开始" class="inputbut" /></td> 
  164.  </tr> 
  165. </table> 
  166. </form> 
  167. <table width="95%" border="0" align="center" cellpadding="3" 
  168.  cellspacing="1" bgcolor="#666666"> 
  169.  <tr bgcolor="#FFFFFF"> 
  170.   <td id="mtd"> 
  171.   <div id='mdv' style='width: 100%; height: 100;'><iframe name="stafrm" 
  172.    frameborder="0" id="stafrm" width="100%" height="100%"></iframe></div> 
  173.   <script type="text/javascript"> 
  174.     document.all.mdv.style.pixelHeight = screen.height - 450; 
  175.     </script></td> 
  176.  </tr> 
  177. </table> 
  178. </body> 
  179. </html> 
  180.  
(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容