Jump to content

[SOLVED] Listing Files and Folders in a Directory


MasterACE14

Recommended Posts

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

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.

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."

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.