天天看點

php實作按檔案名搜尋檔案的遠端檔案查找器

對于本地,我們可以利用windows自帶的查找去進行查找,但是對于線上的話,如查找ftp空間裡面檔案,本程式是很有用的。

使用效果:

php實作按檔案名搜尋檔案的遠端檔案查找器

php檔案查找器源碼:

複制代碼 代碼如下:

<html>

 <head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>php版檔案查找(file search)</title>

 </head>

 <body>

  <form action="" method="post">

  <p> 檔案查找(注:區分大小寫)</p>

  <p>路徑:<input type="text" name="path" /></p>

  <p>查找:<input type="text" name="key" /></p>

  <p><input type="submit" name="sub" value=" 開 始 " /></p>

  </form>

 </body>

</html>

<?php

if(!empty($_POST['path'])&&!empty($_POST['key'])){

 echo "在路徑 ".$_POST['path']."/ 中查找 ".$_POST['key']." 的結果為:<hr/>";

 $file_num = $dir_num = 0;

 $r_file_num = $r_dir_num= 0;

 $findFile = $_POST['key'];

 function delDirAndFile( $dirName ){ 

  if ( $handle = @opendir( "$dirName" ) ) {

   while ( false !== ( $item = readdir( $handle ) ) ) { 

    if ( $item != "." && $item != ".." ) { 

     if ( is_dir( "$dirName/$item" ) ) { 

      delDirAndFile( "$dirName/$item" );

     } else { 

      $GLOBALS['file_num']++;

      if(strstr($item,$GLOBALS['findFile'])){

       echo " <span><b> $dirName/$item </b></span><br />\n";

       $GLOBALS['r_file_num']++;

      }

     } 

    }

   }

   closedir( $handle ); 

   $GLOBALS['dir_num']++;

   if(strstr($dirName,$GLOBALS['findFile'])){

    $loop = explode($GLOBALS['findFile'],$dirName);

    $countArr = count($loop)-1;

    if(empty($loop[$countArr])){

     echo " <span style='color:#297C79;'><b> $dirName </b></span><br />\n";

     $GLOBALS['r_dir_num']++;

    }

   }

  }else{

   die("沒有此路徑!");

  }

 }

 delDirAndFile($_POST['path']);

 echo "<hr/>本次共搜尋到".$file_num."個檔案,檔案夾".$dir_num."個<br/>";

 echo "<hr/>符合結果的共".$r_file_num."個檔案,檔案夾".$r_dir_num."個<br/>";

}

?>