newsuper Posted August 22, 2008 Share Posted August 22, 2008 Hi all I have a working script which works very nicely listing all the files within any subfolders on a network drive. It then displays each folders files as a hyperlink to that file. The problem I am having is that it seems to display the list of files from oldest to newest, whereas I would like to do it alphabetically. I know I need some sort of array, but I'm having some trouble figuring out exactly where/how to do it. Any help appreciated. Code below: function folderlist(){ $startdir = '//officeserver/data/'; $ignoredDirectory[] = '.'; $ignoredDirectory[] = '..'; if (is_dir($startdir)){ if ($dh = opendir($startdir)){ while (($folder = readdir($dh)) !== false){ if (!(array_search($folder,$ignoredDirectory) > -1)){ if (filetype($startdir . $folder) == "dir"){ $directorylist[$startdir . $folder]['name'] = $folder; $directorylist[$startdir . $folder]['path'] = $startdir; } } } closedir($dh); } } return($directorylist); } $folders = folderlist(); ///$rows = 0; foreach ($folders as $folder) { $path = $folder['path']; $name = $folder['name']; echo "<tr>"; ?><td style="width: 10px; height: 25px; background-color:#06546B; border: 1px solid #000;"> <font face="arial" size="0.5" color="#FFD129"><? echo $name; echo "</td>"; // Define the full path to your folder from root $path = "//officeserver/DATA/$name/"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { $file2=urlencode($file); $file3=str_replace('+','%20',$file2); if($file == "." || $file == ".." || $file == "allresearch.php" ) continue; $bgColor = 'white'; $textColor = 'black'; if (strstr($file, "Sell") !== false) { // or stristr() if you want case insensitivity $bgColor = 'red'; $textColor = 'white'; } elseif (strstr($file, "Buy") !== false) { // see above $bgColor = 'blue'; $textColor = 'white'; } echo ' <td bgcolor="'.$bgColor.'" style="border: 1px solid #000;"> <font face="arial" size="0.5" color="'.$textColor.'"> <a target="_blank" style="color: '.$textColor.'; text-decoration: none; background: '.$bgColor.'" href="file:///f:/DATA/'.$name.'/'.$file3.'"> '.substr_replace($file, "", -4).' </a> </font> </td> '; } echo "</tr>"; closedir($dir_handle); } Link to comment https://forums.phpfreaks.com/topic/120788-alphbetically-sorting-a-file-list/ Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 Well I won't do it for you, because I'm tired. But I'll let you know that you'll need to loop through the files and add them all to an array. Then you need to use sort() on that array then use another loop to print out all the files, in alphabetical order. Good luck. Link to comment https://forums.phpfreaks.com/topic/120788-alphbetically-sorting-a-file-list/#findComment-622659 Share on other sites More sharing options...
newsuper Posted August 22, 2008 Author Share Posted August 22, 2008 Yeah I know I need an array and I know I would then sort the array and then display the data, however, because there are actually two arrays - the first array picks up the list of sub-folders in the folder and the second array is the contents of each sub-folder. I know how to do an array sort, but I'm having trouble because there are two arrays here. Link to comment https://forums.phpfreaks.com/topic/120788-alphbetically-sorting-a-file-list/#findComment-622704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.