Andrei Posted March 9, 2009 Share Posted March 9, 2009 Hi, I am a total php neophyte. I hope you can help. I am using the following code to display the content of a folder: // Define the full path to your folder from root $path = "/home/folder01"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" || $file == "error_log" || $file == ".htaccess" || $file == "getfile.php" ) continue; echo "<a href=\"$file\">$file</a><br>"; } // Close closedir($dir_handle); At the moment it displays the files randomly and I'd like to see the files alphabetized. Can anyone suggest how to do this? Thank you so much for your help! Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/ Share on other sites More sharing options...
jackpf Posted March 9, 2009 Share Posted March 9, 2009 Just as a thought, you could put the files into an array and then use sort() to sort them alphabetically..? Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780621 Share on other sites More sharing options...
Andrei Posted March 9, 2009 Author Share Posted March 9, 2009 Not sure how to do that... What would that look like? Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780624 Share on other sites More sharing options...
premiso Posted March 9, 2009 Share Posted March 9, 2009 glob <?php $path = "/home/folder01/*"; $files = glob($path); sort($files); $badNames = array(".", "..", "index.php", "error_log", ".htaccess", "getfile.php"); foreach ($files as $filename) { if (!in_array($filename, $badNames)) { echo '<a href="' . $filename . '">' . $filename . '</a><br />'; } } ?> Should do it for you. Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780630 Share on other sites More sharing options...
jackpf Posted March 9, 2009 Share Posted March 9, 2009 Looks good Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780634 Share on other sites More sharing options...
Andrei Posted March 9, 2009 Author Share Posted March 9, 2009 Thanks, that does indeed alphabetize the files, but it now displays the entire path to the file, as opposed to just the file name (previous code showed just the file name). Can that be remedied? Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780637 Share on other sites More sharing options...
premiso Posted March 9, 2009 Share Posted March 9, 2009 <?php $path = "/home/folder01/*"; $files = glob($path); sort($files); $badNames = array(".", "..", "index.php", "error_log", ".htaccess", "getfile.php"); foreach ($files as $filename) { $filename = explode("/", $filename); $filename = end($filename); if (!in_array($filename, $badNames)) { echo '<a href="' . $filename . '">' . $filename . '</a><br />'; } } ?> Should fix that. Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780642 Share on other sites More sharing options...
thebadbad Posted March 9, 2009 Share Posted March 9, 2009 Or use $filename = basename($filename); instead of $filename = explode("/", $filename); $filename = end($filename); Same outcome, just a bit shorter. And you can remove the dot and double dot from the $badNames array, since they don't appear when using glob(). Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780649 Share on other sites More sharing options...
premiso Posted March 9, 2009 Share Posted March 9, 2009 Yep, a lot better than my way. My mind has just been shot the past few days. Thanks for pointing that out. Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780657 Share on other sites More sharing options...
Andrei Posted March 9, 2009 Author Share Posted March 9, 2009 Wow thanks guys! I appreciate the amazingly fast responses! Works like a charm now! Link to comment https://forums.phpfreaks.com/topic/148656-solved-alphabetize/#findComment-780661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.