Back PHP

List Directory Content using PHP

WRITTEN BY ON 06 Jan 2011
11,117 VIEWS • SHARES
1 comment

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);
  		}
	}
}

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

1 comment
steven 5 years ago
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