PHP : Function Reference : Standard PHP Library (SPL) Functions : DirectoryIterator::isFile
philipp w. gnurepublic_at_linuxmail.org
to actually sort a directoryiterator you need to subclass the iterator and use a comparator function similar to this one
<?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();
}
}
?>
dieseldriver
shows all .jpg files in the current directory but how does the DirectoryIterator sort the output!?
$dir=new DirectoryIterator("./");
foreach ($dir as $file) {
if ($dir->isDot()) {continue;} //removes . and ..
if (strripos($file,".jpg")==true) {
echo $file . " \n";
}
}
|