Example #1
0
function tidy_structure(&$examples, $order)
{
    for ($i = count($examples) - 1; $i >= 0; $i--) {
        if ($examples[$i]['type'] === 'dir' && count($examples[$i]['files']) === 0) {
            // Remove any directories which are empty
            array_splice($examples, $i, 1);
        } else {
            if ($examples[$i]['type'] === 'dir' && !has_files($examples[$i]['files'])) {
                // Remove any directories have only directories for children
                array_splice($examples, $i, 1);
            } else {
                if ($examples[$i]['type'] === 'dir') {
                    tidy_structure($examples[$i]['files'], $order);
                    // Order the examples
                    usort($examples[$i]['files'], function ($a, $b) {
                        if ($a['order'] === $b['order']) {
                            return 0;
                        }
                        return $a['order'] < $b['order'] ? -1 : 1;
                    });
                }
            }
        }
    }
    // Order the top level directories
    usort($examples, function ($a, $b) use($order) {
        if ($a['type'] === 'file' && $b['type'] === 'file') {
            return 0;
        } else {
            if ($a['type'] === 'file' || $b['type'] === 'file') {
                return -1;
            } else {
                $idxA = array_search($a['name'], $order);
                $idxB = array_search($b['name'], $order);
                return $idxA === $idxB ? 0 : $idxA < $idxB ? -1 : 1;
            }
        }
    });
}
    echo 'Directory does not exist.';
    exit;
}
//遍历目录取得文件信息
$file_list = array();
if ($handle = opendir($current_path)) {
    $i = 0;
    while (false !== ($filename = readdir($handle))) {
        if ($filename[0] == '.') {
            continue;
        }
        $file = $current_path . $filename;
        if (is_dir($file)) {
            $file_list[$i]['is_dir'] = true;
            //是否文件夹
            $file_list[$i]['has_file'] = has_files($file);
            //文件夹是否包含文件
            $file_list[$i]['filesize'] = 0;
            //文件大小
            $file_list[$i]['is_photo'] = false;
            //是否图片
            $file_list[$i]['filetype'] = '';
            //文件类别,用扩展名判断
        } else {
            $file_list[$i]['is_dir'] = false;
            $file_list[$i]['has_file'] = false;
            $file_list[$i]['filesize'] = filesize($file);
            $file_list[$i]['dir_path'] = '';
            $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
            $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
            $file_list[$i]['filetype'] = $file_ext;