networkthis Posted August 27, 2008 Share Posted August 27, 2008 I am having a difficult time alphabetically sorting the following code that I have written. I have tried to make this into an array and then sort it but everytime I try I come up empty...any help would be greatly appreciated. I can get a basic file list to sort alphabetically but for some reason this is killing me. Thanks... <?php function showFolder($path){ # Open the Directory Path if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { #*****************************************# # Remove Files that are named . , .. and # # any others we wish to add that we don't # # want displayed in the main directory. # #*****************************************# if ($file != "." && $file != "..") { # Fname will be used to simply copy the Actual Name of the File!!! $fName = $file; # Create the path for the file $file = $path.'/'.$file; #*****************************************# # Simply checks to see if the $file is # # actually a DIRECTORY. If it is display # # the following. # #*****************************************# if (is_dir($file)) { print "<tr><td colspan='2'><img src='../style/dir2.gif' width='16' height='16' alt='dir'/> <a href='".$_SERVER['PHP_SELF']."?path=$file'>$fName</a></td></tr>"; } } } closedir($handle); } } ?> Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/ Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 You probably want to look at the glob() function, which returns the files in an array sorted already. Ken Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/#findComment-626604 Share on other sites More sharing options...
networkthis Posted August 28, 2008 Author Share Posted August 28, 2008 is there another way this can be done such as turning the files into an array and simply using php sort Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/#findComment-627416 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 Why go through all the hassle when glob() does it for you? Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/#findComment-627417 Share on other sites More sharing options...
networkthis Posted August 28, 2008 Author Share Posted August 28, 2008 I am just familiar with using them as an array then using a foreach loop and sorting....... I wrote the above slightly different and just had all my code finished with outputing on the pages as I wished minus the alphabeticall part.......... I have never even heard of the glob() function but I guess I will be checking it out on php.net now to see what it does and how it works. Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/#findComment-627419 Share on other sites More sharing options...
DarkWater Posted August 28, 2008 Share Posted August 28, 2008 foreach (glob('*') as $filename) { } And if you want only directories: foreach (glob('*', GLOB_ONLYDIR) as $filename) { } Link to comment https://forums.phpfreaks.com/topic/121504-sorting-list-in-file-directory/#findComment-627424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.