tags:

views:

515

answers:

1

Hi,

I'm making a directory listing PHP5 script for lighttpd. In a given directory, I'd like to be able to list direct sub-directories and files (with informations).

After a quick search, DirectoryIterator seems to be my friend:

foreach (new DirectoryIterator('.') as $file)
{
    echo $file->getFilename() . '<br />';
}

but I'd like to be able to sort files by filename, date, mime-types...etc

How to do this (with ArrayObject/ArrayIterator?) ?

Thanks

A: 

Philipp W. posted a good example here: http://php.oregonstate.edu/manual/en/directoryiterator.isfile.php

function cmpSPLFileInfo( $splFileInfo1, $splFileInfo2 )
{
    return strcmp( $splFileInfo1->getFileName(), $splFileInfo2->getFileName() );
}

class DirList extends RecursiveDirectoryIterator
{
    private $dirArray;

    public function __construct( $p )
    {
        parent::__construct( $p );
        $this->dirArray = new ArrayObject();
        foreach( $this as $item )
        {
            $this->dirArray->append( $item );
        }
        $this->dirArray->uasort( "cmpSPLFileInfo" );
    }

    public function getIterator()
    {
        return $this->dirArray->getIterator();
    }

}
KiNgMaR