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

最模板

当前位置: 首页 > 建站教程 > 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. ?> 

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