function recursion_dir($path) { static $depth = 0; $dir = opendir($path); while (($file = readdir($dir)) !== false) { if ($file == '.' || $file == '..') { continue; } echo str_repeat("-", $depth) . " {$file}<br />"; if (is_dir("{$path}/{$file}")) { $depth++; recursion_dir("{$path}/{$file}"); $depth--; } } closedir($dir); }
function path_search($path, $search, $is_content = false, $file_ext = '', $is_case = false) { $ext_arr = explode("|", $file_ext); recursion_dir($path, $dirs, $files, -1, 0); $strpos = 'stripos'; //是否区分大小写 if ($is_case) { $strpos = 'strpos'; } $filelist = array(); $folderlist = array(); foreach ($files as $f) { $ext = get_path_ext($f); $path_this = get_path_this($f); if ($file_ext != '' && !in_array($ext, $ext_arr)) { continue; } //文件类型不在用户限定内 if ($strpos($path_this, $search) !== false) { //搜索文件名;搜到就返回;搜不到继续 $filelist[] = file_info($f); continue; } if ($is_content && is_file($f)) { $fp = fopen($f, "r"); $content = @fread($fp, get_filesize($f)); fclose($fp); if ($strpos($content, iconv_app($search)) !== false) { $filelist[] = file_info($f); } } } if ($file_ext == '') { //没限定扩展名则才搜索文件夹 foreach ($dirs as $f) { $path_this = get_path_this($f); if ($strpos($path_this, $search) !== false) { $folderlist[] = array('name' => iconv_app(get_path_this($f)), 'path' => iconv_app(get_path_father($f))); } } } return array('folderlist' => $folderlist, 'filelist' => $filelist); }
function path_search($path, $search, $is_content = false, $file_ext = '', $is_case = false) { $ext_arr = explode("|", $file_ext); recursion_dir($path, $dirs, $files, -1, 0); $strpos = 'stripos'; //是否区分大小写 if ($is_case) { $strpos = 'strpos'; } $filelist = array(); $folderlist = array(); foreach ($files as $f) { $ext = get_path_ext($f); $path_this = get_path_this($f); if ($file_ext != '' && !in_array($ext, $ext_arr)) { continue; } //User-defined file type is not within if ($strpos($path_this, $search) !== false) { //Search for file names; search to return; not search continues $filelist[] = file_info($f); continue; } if ($is_content && is_file($f)) { $fp = fopen($f, "r"); $content = @fread($fp, get_filesize($f)); fclose($fp); if ($strpos($content, iconv_app($search)) !== false) { $filelist[] = file_info($f); } } } if ($file_ext == '') { //Extension is not limited only to search folders foreach ($dirs as $f) { $path_this = get_path_this($f); if ($strpos($path_this, $search) !== false) { $folderlist[] = array('name' => iconv_app(get_path_this($f)), 'path' => iconv_app(get_path_father($f))); } } } return array('folderlist' => $folderlist, 'filelist' => $filelist); }