jay1318 Posted July 24, 2009 Share Posted July 24, 2009 First of all - wonderful site! Great people and very helpful for a newbie like me. I have a simple php script that shows the files in a particular folder. Its working great but I need the results to return alphabetized. Much thanks in advance! Here's my code... <?PHP $folder = "/my/complete/path/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while ($file = readdir($handle)) { $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { if($file == "." || $file == ".." || $file == "index.php" || $file == "thisfilename.php") continue; echo "<li><a href=$file>$file</a></li>"."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/ Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 sort Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882127 Share on other sites More sharing options...
The Eagle Posted July 24, 2009 Share Posted July 24, 2009 EDIT: Maq beat me to posting it. Haha, anyways. If you want it in ascending order use, rsort() Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882128 Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 An empty array and a variable by itself with incorrect syntax. What exactly is that supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882132 Share on other sites More sharing options...
jay1318 Posted July 24, 2009 Author Share Posted July 24, 2009 That's it? Cool. But, can you possibly tell me where exactly to place it in the code or what it might replace? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882133 Share on other sites More sharing options...
vineld Posted July 24, 2009 Share Posted July 24, 2009 http://php.net/sort Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882168 Share on other sites More sharing options...
The Eagle Posted July 24, 2009 Share Posted July 24, 2009 http://php.net/sort Maq already posted that Vineld, ha. That's it? Cool. But, can you possibly tell me where exactly to place it in the code or what it might replace? Thanks again! Take this example jay, $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882172 Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 That's it? Cool. But, can you possibly tell me where exactly to place it in the code or what it might replace? Thanks again! Considering that sort takes an array, you may want to give it the array with the folder names. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882205 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2009 Share Posted July 24, 2009 I would use the glob function, since it returns the files in the directory already sorted by default. Ken Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882208 Share on other sites More sharing options...
jay1318 Posted July 24, 2009 Author Share Posted July 24, 2009 Well, still having trouble. This script is actually one I found online somewhere. Only modified it to not show the name of the php file itself. Not sure why its returning the files out of order. Figured they'd be alphabetized by default. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882253 Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 If you don't use kenrdnsn's solution, then you're going to have to invoke the sort method on the array that contains all the file names ($files). glob is made for such a task, you should probably use it instead. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882260 Share on other sites More sharing options...
jay1318 Posted July 24, 2009 Author Share Posted July 24, 2009 In reading about the glob function, it does indeed appear to be the way to go. Unfortunately, as such a php novice, having trouble incorporating it correctly into the current script. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882282 Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 In reading about the glob function, it does indeed appear to be the way to go. Unfortunately, as such a php novice, having trouble incorporating it correctly into the current script. What's the problem you're having? Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882283 Share on other sites More sharing options...
jay1318 Posted July 24, 2009 Author Share Posted July 24, 2009 Where exactly to put it. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882293 Share on other sites More sharing options...
lonewolf217 Posted July 24, 2009 Share Posted July 24, 2009 that depends what you want to do with it. you can find dozens of examples on google here is a simplistic version of what glob can do <?php $files = glob("C:\*.*"); echo "<pre>"; print_r($files); echo "</pre>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-882380 Share on other sites More sharing options...
Maq Posted July 27, 2009 Share Posted July 27, 2009 Missing a semi-colon after your second echo. Quote Link to comment https://forums.phpfreaks.com/topic/167301-help-with-alphabetizing-folder-contents/#findComment-883875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.