Code Snippet > PHP

List Directory Content using PHP

This function able to loop through the folder you specify and display the content. Each file will be A tagged for quick access

PHP

function list_files($dir)
{
	if(is_dir($dir))
  	{
  		if($handle = opendir($dir))
  		{
  			while(($file = readdir($handle)) !== false)
  			{
  				if($file != "." && $file != ".." && $file != "Thumbs.db")
  				{
  					echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."
";
  				}
  			}
  			closedir($handle);
  		}
	}
}

This code snippet was submitted by:

Kevin Liew is a web designer and developer and keen on contributing to the web development industry. He loves frontend development and absolutely amazed by jQuery. Feel free to say hi to me, or follow @quenesswebblog on twitter.



Show Some Love, Spread This Post!

1 comment

steven Fri, 7th January 2011 improved:

function list_files($dir) {
$files = array();
foreach(glob($dir) as $file) {
$name = end(explode(DIRECTORY_SEPARATOR, $file));
array_push($files, "<a href='$file'>$name</a>");
}
return $files;
}

: )
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement