LemonInflux Posted September 6, 2007 Share Posted September 6, 2007 How do I modificate this code to show subdirectories? <?php $files = array(); $dir=opendir($f_user); while(($file = readdir($dir)) !== false) { if($file !== '.' && $file !== '..' && !is_dir($file)) { $files[] = $file; } } closedir($dir); natcasesort($files); echo "<ul>\n"; for($i=0; $i<count($files); $i++) { if($files[$i] != "index.php") echo '<li><a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delete.php?del='. $files[$i] .'>Delete</a></li>'; } echo "</ul>\n"; ?> $f_user is the username of the person (The user's directory is named '$f_user'). I just want to know how to list them above the files. I can link them up and sort the rest, it's just that bit. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/ Share on other sites More sharing options...
GingerRobot Posted September 6, 2007 Share Posted September 6, 2007 Err, something along the lines of: <?php $files = array(); $dirs = array(); $dir=opendir($f_user); while(($file = readdir($dir)) !== false) { if($file !== '.' && $file !== '..') { if(!is_dir($file)){ $files[] = $file; }else{ $dirs[] = $file; } } You can then proceed to sort the directories as you do the files, and display those above. Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343034 Share on other sites More sharing options...
LemonInflux Posted September 6, 2007 Author Share Posted September 6, 2007 didn't work :\ Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343057 Share on other sites More sharing options...
Psycho Posted September 6, 2007 Share Posted September 6, 2007 Quote didn't work :\ To recieve good information requires good input. What do you mean by "didn't" work? Did you get an error? Are the arrays empty? Ginger left off the closing bracket for the while loop, but that should have been obvious. Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343062 Share on other sites More sharing options...
LemonInflux Posted September 6, 2007 Author Share Posted September 6, 2007 EDIT: Not blank, said that wrong >_< I mean the folders weren't listed. Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343089 Share on other sites More sharing options...
GingerRobot Posted September 6, 2007 Share Posted September 6, 2007 Try: <?php $files = array(); $dirs = array(); $dir=opendir($f_user); while(($file = readdir($dir)) !== false) { if($file !== '.' && $file !== '..') { if(!is_dir($f_user.'\\'.$file)){ $files[] = $file; }else{ $dirs[] = $file; } } } closedir($dir); natcasesort($files); natcasesort($dirs); foreach($dirs as $v){ array_unshift($files,$v); } echo "<ul>\n"; for($i=0; $i<count($files); $i++) { if($files[$i] != "index.php") echo '<li><a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delete.php?del='. $files[$i] .'>Delete</a></li>'; } echo "</ul>\n"; ?> I changed what is passed to the is_dir() function to make it absolute, and added everything in the $dirs array to the beginning of your $files array to make things nice and easy Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343102 Share on other sites More sharing options...
LemonInflux Posted September 6, 2007 Author Share Posted September 6, 2007 Yeah, that worked fine. But is there a way I can change the colour? Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343104 Share on other sites More sharing options...
LemonInflux Posted September 6, 2007 Author Share Posted September 6, 2007 Sorry, didn't specify. Is there a way to make links to folders a different colour? Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343109 Share on other sites More sharing options...
GingerRobot Posted September 6, 2007 Share Posted September 6, 2007 Sure. Instead of adding everything in the $dirs array to the beginning of the $files array, it'll probably be easier to cycle through the two separately. You can then echo out differant css classes for directories and for files. Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343117 Share on other sites More sharing options...
LemonInflux Posted September 6, 2007 Author Share Posted September 6, 2007 so the same kind of thing as echo '<li><a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delete.php?del='. $files[$i] .'>Delete</a></li>';, but with directories instead? Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343119 Share on other sites More sharing options...
GingerRobot Posted September 6, 2007 Share Posted September 6, 2007 Yep. Quote Link to comment https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343121 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.