matthew9090 Posted June 11, 2011 Share Posted June 11, 2011 how would you display folders on top of files part of my code: while (($file = readdir($dir)) !== false) { if (!strstr($file, ".")) //get folders { echo "<a href='index.php?dir=$dir/$file'>" . $file . "</a><br />"; } if ($file!="." && $file!=".." && strstr($file, ".")) { echo $file . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/239057-display-folders-on-top-of-files/ Share on other sites More sharing options...
requinix Posted June 11, 2011 Share Posted June 11, 2011 Get an array of directories (using is_dir) and one of files (is_file) beforehand, so you can do all the pre-processing you want, and then start displaying them. Quote Link to comment https://forums.phpfreaks.com/topic/239057-display-folders-on-top-of-files/#findComment-1228282 Share on other sites More sharing options...
matthew9090 Posted June 11, 2011 Author Share Posted June 11, 2011 but now if i do <?php while (($files = readdir($dir2)) !== false) { if (is_dir($files)) { if ($files != "." && $files != "..") { echo $files . "<br />"; } } elseif (is_file($files)) { echo $files . "<br />"; } } ?> then it only comes up with 1 directory and 1 file, but last time it came up with everything Quote Link to comment https://forums.phpfreaks.com/topic/239057-display-folders-on-top-of-files/#findComment-1228286 Share on other sites More sharing options...
wildteen88 Posted June 11, 2011 Share Posted June 11, 2011 You need to add each directory/file the code finds to a seperate array and then iterate over the directory/files array to display the directory/files in the order you want. example untested code $directories = array(); $files = array(); while (($file = readdir($dir2)) !== false) { if ($file != "." && $file != "..") { if (is_dir($file)) { $directories[] = $file; } elseif (is_file($file)) { $files[] = $file; } } } echo '<pre>' . print_r($directories, true) . '</pre>'; echo '<pre>' . print_r($files, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/239057-display-folders-on-top-of-files/#findComment-1228288 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.