Reputation: 147
I am using the below php codes to get all files from a folder logfiles_patient
$path = "logfiles_patient/";
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle))
{
if(($file!='.')&&($file!='..'))
{
echo "<a target='_blank' href='log_Patient_download.php?filename=$file'>$file</a>";
}
}
// Close
closedir($dir_handle);
and the output echoed is
March 19, 2014.txt
March 20, 2014.txt
March 21, 2014.txt
I want to rearrange the the output as
March 21, 2014.txt
March 20, 2014.txt
March 19, 2014.txt
Upvotes: 1
Views: 115
Reputation: 5840
I suggest you try something like this:
$files = glob('logfiles_patient/*');
if(is_array($files)){
foreach ($files as $file){
$coll[basename($file)] = filemtime($file);
}
asort($coll);
$files = array_keys($coll);
}
Bear in mind that if glob()
encounters an error, it will return false
.
Upvotes: 3
Reputation: 331
You can store temporarily your $file var
in an array
, and applying sorting functions after the while
loop.
Like this:
$array = array();
while ($file = readdir($dir_handle))
{
if(($file!='.')&&($file!='..'))
$array[] = $file;
}
$array = arsort($array);
foreach($array as $file)
echo "<a target='_blank' href='log_Patient_download.php?filename=$file'>$file</a>";
Upvotes: 1