Cristian Posted February 9, 2012 Share Posted February 9, 2012 Sorry for the noobie question, I am just a beginner still. I am trying to use the readdir function to echo back all the contents of a folder. The directory that I want to read back has 31 empty folders. I am able to get them to read back, but they are in a random order and there are two additional files at the top named "." and ".." -- any idea how to fix these? Here is my code and pictures: $path = "c:/test/"; if(is_dir($path)) { if($dir_handle = opendir($path)) { while($filename = readdir($dir_handle)) { echo "filename: {$filename}<br>"; } closedir($dir_handle); } } Quote Link to comment https://forums.phpfreaks.com/topic/256720-readdir-question/ Share on other sites More sharing options...
Psycho Posted February 9, 2012 Share Posted February 9, 2012 The '.' and '..' are special links within the file system that allow you to traverse up the directory structure. You can simply add some if() conditions in your logic to skip them (see example #2 in the manual). Or, alternatively, you can use glob() - which is my preference when reading files/folders. It simply returns an array and never includes those two special links. As for the order - it is not random. It is in alphanumeric order. Windows has logic to do a "natual" sort how a human would order text with trailing numbers. PHP has a couple functions to do this type of sorting on an array. So, either use readdir() to dump the contents into an array first or used glob(). Then use either natsort() [case sensitive] or natcasesort() [case insensitive] to sort the array before outputting it. Quote Link to comment https://forums.phpfreaks.com/topic/256720-readdir-question/#findComment-1316063 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.