Пример #1
0
/**
 * Get a list of files in a directory with metadata.
 *
 * @param $dir
 *   The directory to scan.
 * @param $sort
 *   Sorting parameter. Possible values: name, type, size, date. Defaults to 'name'.
 * @return An array of files. Each item is an array:
 *   array(
 *     'name' => '', // File name.
 *     'shortname' => '', // File name.
 *     'type' => '', // 'file' or 'dir'.
 *     'ext' => '', // File extension.
 *     'writeable' => '', // TRUE if writeable.
 *     'perms' => '', // Permissions.
 *     'modified' => '', // Last modified. Unix timestamp.
 *     'size' => '', // File size in bytes.
 *     'extras' => '' // Array of extra classes for this file.
 *   )
 */
function ft_get_filelist($dir, $sort = 'name')
{
    $filelist = array();
    $subdirs = array();
    if (ft_check_dir($dir) && ($dirlink = @opendir($dir))) {
        // Creates an array with all file names in current directory.
        while (($file = readdir($dirlink)) !== false) {
            if ($file != "." && $file != ".." && (!is_dir("{$dir}/{$file}") && ft_check_file($file) && ft_check_filetype($file) || is_dir("{$dir}/{$file}") && ft_check_dir("{$dir}/{$file}"))) {
                // Hide these two special cases and files and filetypes in blacklists.
                $c = array();
                $c['name'] = $file;
                // $c['shortname'] = ft_get_nice_filename($file, 20);
                $c['shortname'] = $file;
                $c['type'] = "file";
                $c['ext'] = ft_get_ext($file);
                $c['writeable'] = is_writeable("{$dir}/{$file}");
                // Grab extra options from plugins.
                $c['extras'] = array();
                $c['extras'] = ft_invoke_hook('fileextras', $file, $dir);
                // File permissions.
                if ($c['perms'] = @fileperms("{$dir}/{$file}")) {
                    if (is_dir("{$dir}/{$file}")) {
                        $c['perms'] = substr(base_convert($c['perms'], 10, 8), 2);
                    } else {
                        $c['perms'] = substr(base_convert($c['perms'], 10, 8), 3);
                    }
                }
                $c['modified'] = @filemtime("{$dir}/{$file}");
                $c['size'] = @filesize("{$dir}/{$file}");
                if (ft_check_dir("{$dir}/{$file}") && is_dir("{$dir}/{$file}")) {
                    $c['size'] = 0;
                    $c['type'] = "dir";
                    if ($sublink = @opendir("{$dir}/{$file}")) {
                        while (($current = readdir($sublink)) !== false) {
                            if ($current != "." && $current != ".." && ft_check_file($current)) {
                                $c['size']++;
                            }
                        }
                        closedir($sublink);
                    }
                    $subdirs[] = $c;
                } else {
                    $filelist[] = $c;
                }
            }
        }
        closedir($dirlink);
        // sort($filelist);
        // Obtain a list of columns
        $ext = array();
        $name = array();
        $date = array();
        $size = array();
        foreach ($filelist as $key => $row) {
            $ext[$key] = strtolower($row['ext']);
            $name[$key] = strtolower($row['name']);
            $date[$key] = $row['modified'];
            $size[$key] = $row['size'];
        }
        if ($sort == 'type') {
            // Sort by file type and then name.
            array_multisort($ext, SORT_ASC, $name, SORT_ASC, $filelist);
        } elseif ($sort == 'size') {
            // Sort by filesize date and then name.
            array_multisort($size, SORT_ASC, $name, SORT_ASC, $filelist);
        } elseif ($sort == 'date') {
            // Sort by last modified date and then name.
            array_multisort($date, SORT_DESC, $name, SORT_ASC, $filelist);
        } else {
            // Sort by file name.
            array_multisort($name, SORT_ASC, $filelist);
        }
        // Always sort dirs by name.
        sort($subdirs);
        return array_merge($subdirs, $filelist);
    } else {
        return "dirfail";
    }
}
Пример #2
0
/**
 * Private function. Searches for file names and directories recursively.
 *
 * @param $dir
 *   Directory to search.
 * @param $q
 *   Search query.
 * @return An array of files. Each item is an array:
 *   array(
 *     'name' => '', // File name.
 *     'shortname' => '', // File name.
 *     'type' => '', // 'file' or 'dir'.
 *     'dir' => '', // Directory where file is located.
 *   )
 */
function _ft_search_find_files($dir, $q)
{
    $output = array();
    if (ft_check_dir($dir) && ($dirlink = @opendir($dir))) {
        while (($file = readdir($dirlink)) !== false) {
            if ($file != "." && $file != ".." && (ft_check_file($file) && ft_check_filetype($file) || is_dir($dir . "/" . $file) && ft_check_dir($file))) {
                $path = $dir . '/' . $file;
                // Check if filename/directory name is a match.
                if (stristr($file, $q)) {
                    $new['name'] = $file;
                    $new['shortname'] = ft_get_nice_filename($file, 20);
                    $new['dir'] = substr($dir, strlen(ft_get_root()));
                    if (is_dir($path)) {
                        if (ft_check_dir($path)) {
                            $new['type'] = "dir";
                            $output[] = $new;
                        }
                    } else {
                        $new['type'] = "file";
                        $output[] = $new;
                    }
                }
                // Check subdirs for matches.
                if (is_dir($path)) {
                    $dirres = _ft_search_find_files($path, $q);
                    if (is_array($dirres) && count($dirres) > 0) {
                        $output = array_merge($dirres, $output);
                        unset($dirres);
                    }
                }
            }
        }
        sort($output);
        closedir($dirlink);
        return $output;
    } else {
        return FALSE;
    }
}