Forum Moderators: coopster
Hopefully some kind person can help me. I need to sort the results in the part marked 'SORT RESULTS NUMERICALLY?' in my script below.
The files in the directory are named like so: 090628-name-of-file.php so I want to show the latest dated file at the top of the results.
Can anyone help me add this functionality please?
Thanks,
osu
<?php
// SHOW DIRECTORY CONTENTS IN BROWSER
// Start list structure
echo '<ul>';
// Define the full path to your folder from root
$path = "/path here";// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");// Loop through the files
while ($file = readdir($dir_handle)) {
// CLEAN FILENAME
// Strip file extension
$title = substr_replace($file,"",-4);// Strip #*$!#*$!- date from beginning of filename
$title = substr($title,7);// Replace '-' with spaces
$title = str_replace("-"," ",$title);// Make it a teaser title
$title = substr($title, 0, 50);// Finally, capitalise each word
$title = ucwords($title);if($file == "." ¦¦ $file == ".." ¦¦ $file == "index.php" )
continue;
// SORT RESULTS NUMERICALLY?
echo '<li>';
echo "$title... <a href=\"$file\">read more</a><br />";
echo '</li>';
}// Close list structure
echo '</ul>';// Close directory
closedir($dir_handle);?>
$mydir = dir($path);
while(($file3 = $mydir->read()) !== false)
{
$ext3=explode('.',$file3); // modify this part if your fileames can contain more than one dot
if($file3 != '.' && $file3 != '..' && $ext3[1] && $file3!="Thumbs.db")
{
$array_thumb[]=$file3;
}
}
if ($array_thumb!="")
{
sort($array_thumb); // this arranges your array
foreach ($array_thumb as $img)
{
// do what you like with your single file
}
}
More info on sort() [us3.php.net]