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 ><b> $dirName </b></span><br />n";
  46. $GLOBALS['r_dir_num']++;
  47. }
  48. }
  49. }else{
  50. die("没有此路径!");
  51. }
  52. }
  53. delDirAndFile($_POST['path']);
  54. echo "<hr/>本次共搜索到".$file_num."个文件,文件夹".$dir_num."个<br/>";
  55. echo "<hr/>符合结果的共".$r_file_num."个文件,文件夹".$r_dir_num."个<br/>";
  56. }
  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. }
  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. 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. }
  64. function isExists($str, $filename, $content) {
  65. global $dangerous;
  66. //sleep ( 1 );
  67. set_time_limit ( 10 );
  68. $arr = explode ( ',', $str );
  69. $signature="特征码:";
  70. if (! emptyempty ( $arr )) {
  71. // $content = file_get_contents ( $filename ); //这个性能较好
  72. $content = strtolower ( $content ); //全部转为小写
  73. $error_count = 0;
  74. foreach ( $arr as $a ) {
  75. if (trim ( $a ) != "") {
  76. if (strpos ( $content, $a )) {
  77. $error_count = $error_count + 1;
  78. $signature.=$a." ";
  79. }
  80. }
  81. }
  82. if ($error_count > 0) {
  83. // $dangerous [] = $filename;
  84. $dangerous [] = "[error] " . $error_count . " " .$signature." " . $filename;
  85. //echo "[error] " . $error_count . " " .$signature." " . $filename . "<br/>";
  86. }else{
  87. //echo "[ok] " . $filename . "<br/>";
  88. }
  89. }
  90. }
  91. ?>
  92. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  93. <html xmlns="http://www.w3.org/1999/xhtml">
  94. <head>
  95. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  96. <title>批量查询文件</title>
  97. <style type="text/css">
  98. body {
  99. background: #FFFFFF;
  100. color: #000;
  101. font-size: 12px;
  102. }
  103. #top {
  104. text-align: center;
  105. }
  106. h1,p,form {
  107. margin: 0;
  108. padding: 0;
  109. }
  110. h1 {font-size; 14px;
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div >
  116. <h1>批量查找程序</h1>
  117. <div>本程序可以扫描指定目录的所有文件,进行<strong>内容查找</strong>。<br />
  118. 在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div>
  119. </div>
  120. <form action="<?=$_SERVER ['SCRIPT_NAME']?>" name="form1"
  121. target="stafrm" method="post">
  122. <table width="95%" 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. 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. size="20" value="htm|html|shtml|php" /> 多个请用|隔开</td>
  133. </tr>
  134. <tr >
  135. <td height="64" colspan="2" bgcolor="#FFFFFF">
  136. <table width="100%" 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"
  149. >小亮,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%" align="center" cellpadding="3"
  161. cellspacing="1" bgcolor="#666666">
  162. <tr bgcolor="#FFFFFF">
  163. <td >
  164. <div ><iframe name="stafrm"
  165. frame 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>