killswitchfilter Posted July 10, 2008 Share Posted July 10, 2008 Im using this script to display directories recursively, but im not smart enough to figure out how to modify it to sort the results alphabetically. i know the function is sort() but i have no idea where to put it. i tried: sort(getDirectory( "./pricing/" )); but obviously that didn't work <?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "<a href=\"$path/$file\">$file</a><br />"; // Print out the link to the file } } } closedir( $dh ); // Close the directory handle } ?> <?php getDirectory( "./pricing/" ); ?> Link to comment https://forums.phpfreaks.com/topic/114093-where-does-sort-go/ Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 You've got to build it a little different if you want sorting. First, loop through all the files and store them in an array, sort the array, then loop through the array <?php $path = 'C:/wamp/www'; $ignore = array( 'cgi-bin', '.', '..' ); $dh = @opendir( $path ); $files = array(); while( false !== ( $file = readdir( $dh ) ) ) if( !in_array( $file, $ignore ) ) $files[] = $file; sort( $files ); // Now loop through file array we've made foreach ( $files as $file ) { $spaces = str_repeat( ' ', ( $level * 4 ) ); if( is_dir( "$path/$file" ) ){ echo "<strong>$spaces $file</strong><br />"; #getDirectory( "$path/$file", ($level+1) ); } else { echo "<a href=\"$path/$file\">$file</a><br />"; } } ?> If you wanna sort by folders, then files, both alphabetically, just check to see if its a directory when you're building the array, and store directories in another array... sort them both, then just foreach the directory array and the files array separately. Link to comment https://forums.phpfreaks.com/topic/114093-where-does-sort-go/#findComment-586421 Share on other sites More sharing options...
killswitchfilter Posted July 14, 2008 Author Share Posted July 14, 2008 thank you very much for your help but this code did not work for me. its no longer a function now, so i don't know how to call it? i just need my recursive directory to list the files and folders in alphabetical order. :'( Link to comment https://forums.phpfreaks.com/topic/114093-where-does-sort-go/#findComment-589680 Share on other sites More sharing options...
killswitchfilter Posted July 14, 2008 Author Share Posted July 14, 2008 Using the code you gave me in my function gives me a nice alphabetical list, but its no longer recursive. I don't know what you mean when you say "sort directories in another array", or "just foreach the directory array". I have no idea what I'm doing (obviously) and it seems I need to hire a professional. Link to comment https://forums.phpfreaks.com/topic/114093-where-does-sort-go/#findComment-589708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.