MasterACE14 Posted November 25, 2008 Share Posted November 25, 2008 I have a function which displays all the files and folders in a given directory. But I'm having trouble with a few things. 1) I want it to display files within folders, in a document tree type way. 2) I have it color coding folders, and files and I want it to leave every other file that isn't a php file or a folder as a red color. Here is my function: <?php // display files function display_folder($dirname) { $dir = opendir($dirname); while( false != ( $file = readdir($dir)) ) { if(($file != ".") and ($file != "..")) { if(substr($file,"-4") != ".php" && !is_dir($file)) { $file_list .= "<li style=\"color: yellow;\">$file</li>"; } if(substr($file,"-4") == ".php") { $file_list .= "<li style=\"color: aqua;\">$file</li>"; } if(is_dir($file)) { $file_list .= "<li style=\"color: red;\">$file</li>"; } } } return $file_list; closedir($dir); } ?> Any help is greatly appreciated. Regards ACE Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/ Share on other sites More sharing options...
Adam Posted November 25, 2008 Share Posted November 25, 2008 What are you having problems with? Adam Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-698449 Share on other sites More sharing options...
MasterACE14 Posted November 25, 2008 Author Share Posted November 25, 2008 here's a small section of the output... # php3.php # php2.php # php1.php # includes I just have no idea how to do a document tree type thing for folders > subfolders > files etc. So the Above with the includes folder would look something like this... # php3.php # php2.php # php1.php # includes - include1.php - include2.php # subfolder - include3.php - include4.php and so on and so forth. I just have no clue as to how I would go about making this document tree? Also, I have some folders displaying as yellow, when they should be red. Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-698461 Share on other sites More sharing options...
MasterACE14 Posted November 25, 2008 Author Share Posted November 25, 2008 bump Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-698900 Share on other sites More sharing options...
MasterACE14 Posted November 27, 2008 Author Share Posted November 27, 2008 *bump* *shake* *tremble* Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-700088 Share on other sites More sharing options...
corbin Posted November 27, 2008 Share Posted November 27, 2008 Bleh, I'll just give you the code, then I'll tell you what was wrong with yours. <?php function display_folder($dirname) { $dir = opendir($dirname); $file_list = ''; while(false !== ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { if(is_dir($dirname . '/' . $file)) { $file_list .= "<li style=\"color: red\">{$file}\n<ul>"; $file_list .= display_folder($dirname . '/' . $file); $file_list .= "</ul>\n</li>\n"; } elseif(substr($file,"-4") == ".php") { $file_list .= "<li style=\"color: aqua;\">$file</li>\n"; } else { $file_list .= "<li style=\"color: yellow;\">$file</li>\n"; } } } closedir($dir); return $file_list; } echo '<ul>'; echo display_folder('test_folder'); echo '</ul>'; Now, your code: <?php // display files function display_folder($dirname) { $dir = opendir($dirname); while( false != ( $file = readdir($dir)) ) { if(($file != ".") and ($file != "..")) { if(substr($file,"-4") != ".php" && !is_dir($file)) { $file_list .= "<li style=\"color: yellow;\">$file</li>"; } if(substr($file,"-4") == ".php") { $file_list .= "<li style=\"color: aqua;\">$file</li>"; } if(is_dir($file)) { $file_list .= "<li style=\"color: red;\">$file</li>"; } } } return $file_list; closedir($dir); } ?> First off, why the hell are you doing an if tree like that? if(a) { } if(b and not a) { } if(not b and not a) { } There are things besides if for a reason! Use them. if(a) elseif(b) else Second. You're looking for a recursive function to achieve the tree effect. Third. Why is closedir() after the return statement? That will never be executed. false != ( $file = readdir($dir)) should be false !== since a file name could be "0" or "false." Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-700100 Share on other sites More sharing options...
MasterACE14 Posted November 27, 2008 Author Share Posted November 27, 2008 woah, my code was poor =/ I have no idea why I didn't use a else anywhere, thanks for pointing that out I didn't realise you could put a function, inside the function where it is created. That's certainly helpful. Thanks for your help and advice corbin. Appreciated Regards ACE Link to comment https://forums.phpfreaks.com/topic/134170-solved-listing-files-and-folders-in-a-directory/#findComment-700109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.