欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 目录递归遍历程序_PHP教程

程序员文章站 2022-05-09 13:04:32
...
一个朋友写的一款目录查找程序,可以根据用户输入的目录名称查到到指定目录或文件,同时还支持锁定目录哦,有需要的朋友可以参考一下。
代码如下 复制代码

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 $pre = '';
$pos = 0;
}

$findata = substr($fd,$pos,$this->previewLen);
$findata = str_replace($this->key,''.$this->key.'',$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]结果:
";
if(!$ret) die('无');
foreach($ret as $p=>$f){
echo "$p. t$f[path] => $f[preview]
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[] = "";
}
$folder_op = implode($folder_op);
?>


搜索关键字:

搜索目录:

是否限定以上选择的目录:

=


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444740.htmlTechArticle一个朋友写的一款目录查找程序,可以根据用户输入的目录名称查到到指定目录或文件,同时还支持锁定目录哦,有需要的朋友可以参考一...