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

?> 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.