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! Quote Link to comment 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..? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 9, 2009 Share Posted March 9, 2009 Looks good Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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(). Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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.