(PHP源码)搜索目录包含子目录的文件

<?php

$search_str = $_GET['url'];

$results = search('.', $search_str);

if(!$results[0]){
	$player=[];
	$player['code']=404;
	$player['msg']='没有搜索到文件';
	$json=json_encode($player,JSON_UNESCAPED_UNICODE);
	print_r($json);
	exit;
}else{
	$player=[];
	$player['code']=200;
	$player['url']=$results[0];
	$json=json_encode($player,JSON_UNESCAPED_UNICODE);
	print_r($json);
	exit;
}



function search($dir, $search_str) {
    $files = scandir($dir);
    $results = array();

    foreach ($files as $file) {
        if ($file == '.' || $file == '..') {
            continue;
        }

        $path = $dir.'/'.$file;

        if (is_dir($path)) {
            $results = array_merge($results, search($path, $search_str));
        } else if (@strpos($file, $search_str) !== false) {
            $results[] = $path;
        }
    }

    return $results;
}

?>
© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论