php 目錄遞迴遍曆程式

來源:互聯網
上載者:User
 代碼如下 複製代碼

<?php
class Finder{
 private $key;
 private $result;
 private $previewLen = 50;
 private $file_type = array('html','php','htm','txt');

 function __construct($key){
  $this->key = $key;
 }

 function find($folder){
  $this->result = array();
  
  if(is_array($folder)){
   foreach($folder as $f){
    $this->_find_in_folder($f);
   }
  }else{
   $this->_find_in_folder($folder, true);
  }

  return $this->result;
 }
 
 function _find_in_folder($folder,$bSub=false){
  foreach(glob($folder.DIRECTORY_SEPARATOR.'*') as $f){
   if (is_file($f)){
    $extend =explode("." , $f);
    $type = strtolower(end($extend));
    if(in_array($type,$this->file_type)){
     $fd = file_get_contents($f);
     $pos = strpos($fd,$this->key);
     if($pos!==false){
      $end = $pre = '...';
      $pos -= floor($this->previewLen/2);
      if($pos<0){
       $pre = '';
       $pos = 0;
      }

      $findata = substr($fd,$pos,$this->previewLen);
      $findata = str_replace($this->key,'<span style="color:red">'.$this->key.'</span>',$findata);
      $this->result[] = array('path'=>$f,'preview'=>$pre.$findata.$end);
     }
    }
    continue;
   }

   if($bSub && is_dir($f)){
    $this->_find_in_folder($f,true);
   }
  }
 }
 
}

$cur_path = dirname(__FILE__);
if(isset($_GET['a'])){
 $key = $_POST['key'];
 if(!$key) die('關鍵字不可為空');

 $cf = new Finder($key);

 $in_folder = array();
 $limit_folder = $_POST['limit_folder'];
 if($limit_folder==1){
  if(!isset($_POST['folder']) || !$_POST['folder']) die('限定目錄不可為空');
  $in_folder = $_POST['folder'];
  $ret = $cf->find($in_folder);
 }else{
  $ret = $cf->find($cur_path);
 }

 echo "搜尋[$key]結果:<br />";
 if(!$ret) die('無');
 foreach($ret as $p=>$f){
  echo "$p. t$f[path] => $f[preview] <br />n";
 }
 exit(); 
}

$folder = array();
function readFolder($path){
 global $folder;
 $folder[] = $path;
 foreach(glob($path.DIRECTORY_SEPARATOR.'*') as $f){
  if (is_dir($f)) {
   readFolder($f);
  }
 }
}

readFolder($cur_path);
$folder_op = array();
foreach($folder as $path){
 $folder_op[] = "<option value="$path">$path</option>";
}
$folder_op = implode($folder_op);
?>
<form action="?a=do" method="post">
搜尋索引鍵:<input type="text" name="key" value=""><br />
搜尋目錄:<select name="folder[]" multiple="true"><?php echo $folder_op ?></select><br />
是否限定以上選擇的目錄:<input type="radio" name="limit_folder" value="1" />是 <input type="radio" name="limit_folder" value="0" checked="true" />否
<input type="submit" value="搜尋" />
</form>

=

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.