示例#1
0
function file_list($path, $include_files = array(), $ignore_files = array(), $setting = NULL)
{
    $result = array();
    $include_files = to_array($include_files);
    $ignore_files = to_array($ignore_files);
    //Format include_files
    foreach ($include_files as $key => $value) {
        $include_files[$key] = normalize_path($value);
    }
    //Format ignore_files
    foreach ($ignore_files as $key => $value) {
        $ignore_files[$key] = normalize_path($value);
    }
    $_setting = array('file_key' => ($setting & FILE_LIST_FILE_KEY) == FILE_LIST_FILE_KEY, 'debug_path' => ($setting & FILE_LIST_DEBUG_PATH) == FILE_LIST_DEBUG_PATH, 'file_info' => ($setting & FILE_LIST_FILE_INFO) == FILE_LIST_FILE_INFO, 'include_folder' => ($setting & FILE_LIST_INCLUDE_FOLDER) == FILE_LIST_INCLUDE_FOLDER, 'subfolder' => ($setting & FILE_LIST_SUBFOLDER) == FILE_LIST_SUBFOLDER);
    $queue = array($path);
    $pt = NULL;
    while (list($k, $path) = each($queue)) {
        //3
        ///*
        if ($handle = opendir($path)) {
            while (FALSE !== ($file = readdir($handle))) {
                //2
                if ($file == '.' || $file == '..') {
                    continue 1;
                }
                $real_path = normalize_path($path . DIRECTORY_SEPARATOR . $file);
                if (is_dir($real_path)) {
                    $_setting['include_folder'] && ($queue[] = $real_path);
                    if (!$_setting['include_folder']) {
                        continue 1;
                    }
                }
                foreach ($include_files as $key => $value) {
                    //1
                    if (!fnmatch($value, $real_path, FNM_CASEFOLD | FNM_NOESCAPE)) {
                        continue 2;
                    }
                }
                foreach ($ignore_files as $value) {
                    //1
                    if (fnmatch($value, $real_path, FNM_CASEFOLD | FNM_NOESCAPE)) {
                        continue 2;
                    }
                }
                if ($_setting['file_key']) {
                    $pt =& $result[anystring2utf8(str_replace('\\', '/', $_setting['debug_path'] ? Debug::path($real_path) : $real_path))];
                } else {
                    $pt =& $result[];
                }
                $pt = $_setting['file_info'] ? fileinfo($real_path) : $real_path;
            }
        }
        closedir($handle);
        //*/
        /*
        		$path .= DIRECTORY_SEPARATOR;
        		$list = glob($path.'{.,}*',GLOB_BRACE | GLOB_NOESCAPE );
        		foreach ($list as $real_path)
        		{
        			if ($real_path == $path.'.' || $real_path == $path.'..') continue 1;
        			$real_path = normalize_path($real_path);
        			foreach($ignore_files as $value)
        			{
        				if (fnmatch($value, $real_path, FNM_PATHNAME | FNM_CASEFOLD | FNM_NOESCAPE))
        					continue 2;
        			}
        			$result[str_replace('\\','/', $debug_path ? Debug::path($real_path) : $real_path)] = $fileinfo = fileinfo($real_path);
        			if ($fileinfo['type'] == 'dir')
        				$queue[] = $real_path;
        		}
        		//*/
    }
    ksort($result);
    //print_r($result);
    return $result;
}
示例#2
0
/**
 * 任何编码字符串(数组)转换为utf-8
 *
 * @param mixed $string 输入字符串(数组)
 * @return mixed 输出utf-8编码字符串(数组)
 */
function any2utf8($string)
{
    if (is_array($string)) {
        foreach ($string as $key => $val) {
            $string[$key] = any2utf8($val);
            //递归
        }
    } else {
        $string = anystring2utf8($string);
    }
    return $string;
}