• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net

[PHP] Directory Script - how do i make it sort accordingly?

Anayet

New Member
I have this directory file listing script i use which somebody wrote for me some time ago:
PHP:
<?php 
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 
            echo "<a href='$file'>$file</a><br>"; 
        } 
    }
    closedir($handle); 
}
?>
What i want to know is, is there some way i can have files sorted automatically by name? And also, is there a way i can have them be sorted by date modified? I use the script in different directories which is why i'm wanting two different methods of sort.

Thanks :classic2:
 
PHP:
// creates a new array of files
$dirpath = 'uploads/';
$dir = opendir($dirpath);
$files = array();
while (false !== ($file = readdir($dir))) {
   $localpath = $dirpath.$file;
   if (is_file($localpath)) {
       $key = filemtime($localpath);
       $files_by_date[$key] = $file;
       $files_by_name[] = $file;
   }
}

// sort files
ksort($files_by_date);
sort($files_by_name);

// use this to displays files by date
foreach ($files_by_date as $file) echo $file.'<br />';

// or use this to displays files by name
foreach ($files_by_name as $file) echo $file.'<br />';

I just wrote this now and haven't tried it, so you should check.
 
Last edited:
I seem to be having problems with the sort by date modified. It only lists a few files.
I've upload here to test: http://www.digitalxtc.co.uk/dunst/
the date.php is the file that only sorts by date modifed, and the name.php by name.

The name.php works perfectly, but the date.php only lists three image files when theres actually six.

Is it because of the way i configured it?
I did this in the date.php:
PHP:
<?php
// creates a new array of files
$dirpath = '../dunst/';
$dir = opendir($dirpath);
$files = array();
while (false !== ($file = readdir($dir))) {
   $localpath = $dirpath.$file;
   if (is_file($localpath)) {
       $key = filemtime($localpath);
       $files_by_date[$key] = $file;
       $files_by_name[] = $file;
   }
}

// sort files
ksort($files_by_date);

// use this to displays files by date
foreach ($files_by_date as $file) echo $file.'<br />';

?>

Thanks
 
Last edited:
i am not sure why its like that, but it works for me. maybe because all your files have the same date and time. but it should still work anyway.

PHP:
$dirpath = '../dunst/';
$dir = opendir($dirpath);

while (false !== ($file = readdir($dir))) {
   $localpath = $dirpath.$file;
   if (is_file($localpath)) {
       $key = filemtime($localpath);
       $files_by_date[$key] = $file;
       $files_by_name[] = $file;
   }
}

ksort($files_by_date);

foreach ($files_by_date as $file) echo $file.'<br />';
 
Last edited:
kabatak said:
i am not sure why its like that, but it works for me. maybe because all your files have the same date and time. but it should still work anyway.
Yep, its because they have the same date/time. I reuploaded one of the files that was not showing up and it showed up (had new modified date).

The name sorting works perfectly though :classic2:

Anyone know how i can get the date sorting above to work? oldman2
 
Back
Top