...

View Full Version : Sort files by name



thedevilspride
09-14-2003, 04:38 PM
So far I have:



<?php


$lstFileNames=array();
$lstFileTitles=array();
$file_names = array();
$lines = file ('/path/to/files/true_names.txt');
foreach ($lines as $line_num => $line)
{
$parts = explode('|',$line);
$file_names[$parts[0]] = $parts[1];
}



if ($handle = opendir('/path/to/files/midis/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {


echo " <tr>";
echo " <td valign=\"top\" colspan=\"3\" width=\"95%\"><b><font color='#6B7B90'>" . $file_names[$file] . "</font></b></td>";
echo " </tr>";




if ($handle2 = opendir('/path/to/files/midis/' . $file)) {
while (false !== ($file2 = readdir($handle2))) {
if ($file2 != "." && $file2 != ".." && $file2 != "index.html") {




$filename = '/path/to/files/midis/' . $file . '/' . $file2;




echo " <tr>";
echo " <td valign=\"top\" width=\"65%\"><a href=\"http://www.thedevilspride.com/files/midis/$file/$file2\">";
echo " " . $file_names[$file2] . "</a></td>";
echo " <td width=\"5%\">&nbsp;</td>";
echo " <td align=\"right\" valign=\"top\" width=\"35%\">" . number_format(filesize($filename)/1024)." KB" . "</td>";
echo " </tr>";

}
}
closedir($handle2);
}
echo "<tr>";
echo " <td valign=\"top\" width=\"65%\">&nbsp;</td>";
echo " <td width=\"5%\">&nbsp;</td>";
echo " <td align=\"right\" valign=\"top\" width=\"35%\">&nbsp;</td>";
echo " </tr>";

}
}
closedir($handle);
}


?>

At the moment they are all sorted by date.
Does anyone know how I could sort the files by their filesnames?
I've tried sort() but not too sure how to get it to work.

missing-score
09-15-2003, 09:56 PM
try using ksort() rather than sort.

thedevilspride
09-15-2003, 10:39 PM
I've got no idea to add that to my existing code :(

I've tried:


if ($handle2 = opendir('/path/to/files/midis/' . $file)) {
while (false !== (ksort($file2 = readdir($handle2)))) {
if ($file2 != "." && $file2 != ".." && $file2 != "index.html") {

also tried adding
ksort($file2); under the above code

Arrays (and sorting them) aren't my speciality

missing-score
09-16-2003, 04:53 PM
$lstFileNames=array();
$lstFileTitles=array();
$file_names = array();
$lines = file ('/path/to/files/true_names.txt');
foreach ($lines as $line_num => $line)
{
$parts = explode('|',$line);
$file_names[$parts[0]] = $parts[1];
}

ksort($file_names);


should put them in alphanumeric, then you can print the filenames from that array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum