PHP Sort Files In Directory by Type

限于喜欢 提交于 2020-01-05 02:26:14

问题


I wrote the following PHP code to display files in a directory. It uses JQuery to expand folders. Everything works fine but right now it displays all files in alphabetical order mixing file types.

I'd like to keep the alphabetical order but display folders and files separately. How can I sort the displayed files so Folders are displayed on top and other files are displayed bellow.

In other words, how do I sort the files by file type?

Thank you very much!

<?php
$path = ROOT_PATH;
$dir_handle = @opendir($path) or die("Unable to open $path");
list_dir($dir_handle,$path);

function list_dir($dir_handle,$path)
{
    echo "<ul class='treeview'>";

    while (false !== ($file = readdir($dir_handle))) 
    {
        $dir =$path.'/'.$file;
        if(is_dir($dir) && $file != '.' && $file !='..' )
        {
            $handle = @opendir($dir) or die("undable to open file $file");
            echo '<li class="folder"><a href="#" class="toggle">'.$file.'</a></li>';
            list_dir($handle, $dir);
        }
        elseif($file != '.' && $file !='..')
        {
            echo '<li class="file"><a href="file-details.php?file='.$dir.'" class="arrow_icon modal">'.$file.'</a></li>';
        }
    }

    echo "</ul>";

    closedir($dir_handle);
}
?>

回答1:


First thing you should do is separate the logic of getting/sorting the files and displaying them, that'll make customisation easier..

Here's a working solution (had some spare time this morning :)

list_dir(ROOT_PATH);

/* Rendering */
function list_dir($path)
{
   $items = get_sorted_entries($path);

    if (!$items)
        return;

    echo "<ul class='treeview'>";


    foreach($items as $item)
    {
        if ($item->type=='dir')
        {
            echo '<li class="folder"><a href="#" class="toggle">'.$item->entry.'</a></li>';
            list_dir($item->full_path);
        }
        else
        {
            echo '<li class="file"><a href="file-details.php?file='.urlencode($item->full_path).'" class="arrow_icon modal">'.$item->entry.'</a></li>';
        }
    }

    echo "</ul>";

}

/* Finding */
function get_sorted_entries($path)
{
    $dir_handle = @opendir($path) ;
    $items = array();

    while (false !== ($item = readdir($dir_handle))) 
    {
        $dir =$path.'/'.$item;
        if ( $item == '.' || $item =='..' )
            continue;

        if(is_dir($dir))
        {
            $items[] = (object) array('type'=>'dir','entry'=>$item, 'full_path'=>$dir);
        }
        else
        {
            $items[] = (object) array('type'=>'file','entry'=>$item, 'full_path'=>$dir);
        }
    }
    closedir($dir_handle);

    usort($items,'_sort_entries');

    return $items;
}

/* Sorting */
function _sort_entries($a, $b)
{
    return strcmp($a->entry,$b->entry);
}

Edit: And if you want to show the directories first, change the sort function to this:

function _sort_entries($a, $b)
{
    if ($a->type!=$b->type)
        return strcmp($a->type,$b->type);

    return strcmp($a->entry,$b->entry);
}

That will put the directories at the top (Windows style)



来源:https://stackoverflow.com/questions/5588798/php-sort-files-in-directory-by-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!