essjay_d12 Posted May 16, 2007 Share Posted May 16, 2007 I am trying to oder folders before files when i display a directory through php. Though they do not seem to work with the following code .... anyone see what's wrong? if (is_dir($dir)){ if ($dh = opendir($dir)) { // removes two hidden files "." and ".." if($file != "." && $file != "..") { while ((($file = readdir($dh)) !== false) && (filetype($dir . $file) == "dir" )) { echo $file; echo 'folder'; } while ((($file = readdir($dh)) !== false) && (filetype($dir . $file) == "file" )) { echo $file; echo 'file'; } } } closedir($dh); } cheers d Quote Link to comment https://forums.phpfreaks.com/topic/51666-while-loops-and-readdir-function-problem/ Share on other sites More sharing options...
lee20 Posted May 16, 2007 Share Posted May 16, 2007 Both your loops have the same 1st condition, so once the first loop finishes the second loop will obviously never execute. while ((($file = readdir($dh)) !== false)... ... while ((($file = readdir($dh)) !== false)... You need to reopen the directory between your loops like so: if (is_dir($dir)){ if ($dh = opendir($dir)) { // removes two hidden files "." and ".." if($file != "." && $file != "..") { while ((($file = readdir($dh)) !== false) && (filetype($dir . $file) == "dir" )) { echo $file; echo 'folder'; } closedir($dh); $dh = opendir($dir); // Reopen the directory while ((($file = readdir($dh)) !== false) && (filetype($dir . $file) == "file" )) { echo $file; echo 'file'; } } } closedir($dh); } Quote Link to comment https://forums.phpfreaks.com/topic/51666-while-loops-and-readdir-function-problem/#findComment-254629 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.