Jump to content

Sorting DirectoryIterator directory list?


simonp

Recommended Posts

I'm using the small bit of code below to show a directory listing of the files in a single folder but I want to be able to sort the files into alphabetical order.

 

I believe this involves putting the output into an array and then showing the sorted array but I've no idea how to do this.

 

Any help greatly appreciated.

 

<?php

$dir = new DirectoryIterator( '/home/');

foreach($dir as $file ) 
{
  if(!$file->isDot() && !$file->isDir()) {
    echo "<br><br>FileName: ".$file->getFilename();
    echo "<br>Size: ".number_format(($file->getSize()/1024),2)." Kb";
    echo "<br>Uploaded: ".date("D d M Y H:i:s",$file->getCTime());
  }
}

?> 

<?php
$dir = new DirectoryIterator( '/home/');
function mySort($a,$b) {
$x = $a[0];
$y = $b[0];
if (strcmp($x,$y) < 0) return -1;
elseif (strcmp($x,$y) === 0) return 0;
else return -1;
}
$files = array();
foreach($dir as $file ) {
  if(!$file->isDot() && !$file->isDir()) {
    $files[] = array($file->getFilename(),number_format($fuke->getSize() / 1024,2),date("D d M Y H:i:s",$file->getCTime()));
  }
}
usort($files,'mySort');
print_r($files);
?>  

sorry went about it all wrong found an easier solution, read the above code, made it in notepad++ so it SHOULD work more than definately

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.