PHP按类型在目录中排序文件 [英] PHP Sort Files In Directory by Type

查看:121
本文介绍了PHP按类型在目录中排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下PHP代码来在目录中显示文件。它使用JQuery扩展文件夹。一切工作正常,但现在它显示所有文件按字母顺序混合文件类型。



我想保留字母顺序,但分别显示文件夹和文件。如何对显示的文件进行排序,因此文件夹显示在顶部,其他文件将显示在下面。



换句话说,如何按文件类型对文件进行排序? / p>

非常感谢!

 <?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)或死(不可打开文件$文件);
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);
}
?>


解决方案

你应该做的第一件事是将获得的逻辑分开/ /排序文件并显示它们,这样可以使自定义更容易。



这是一个工作的解决方案(今天早上有空闲时间)
$ b

  list_dir(ROOT_PATH); 

/ *渲染* /
函数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>;

}

/ *查找* /
函数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;
}

/ *排序* /
函数_sort_entries($ a,$ b)
{
返回strcmp($ a-> $ B->条目);
}

修改:如果你想显示目录首先将排序功能更改为:

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

返回strcmp($ a->条目,$ b->条目);
}

将目录放在顶端(Windows样式)


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);
}
?>

解决方案

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)

这篇关于PHP按类型在目录中排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆