PHP File: Showing All Files and Folders in Particular Directory



PHP File Tips - Part 6: In this post, we will display all file and folders in a directory. We will see directory structure.
<?
function showDir($dir, $depth=0)
{
  if(!is_dir($dir)){
    die("not a directory");
  }
  
  $od = opendir($dir);
  
  while(($file = readdir($od)) !== false){
    
	if($file != "." && $file != "..")
	{
	  if(is_dir("$dir/$file"))
	  {
	    echo "<tr><td>";
	    echo str_repeat("  ", $depth) . " [$file]";
		echo "</td></tr>";
	    showDir("$dir/$file", ($depth + 1));
	  }
	  else
	  {
	    echo "<tr><td>";	  
	    echo str_repeat("  ",$depth) . " $file ";
		echo "</td><td>";
		echo ceil(filesize("$dir/$file")/1000)." KB";
		echo "</td></tr>";		
	  }
	}
	
  }
  
}
?>
<table>
  <tr>
    <th>Directory/File Name</th>
    <th>Size</th>
  </tr>
<?
showDir("F:\help\php_manual_chm_12");
?>
</table>


Series this article:
PHP File: 3 Way of Reading File
PHP File: Reading File at Particular Lines
PHP File: Reading File at Particular Byte Ranges
PHP File: Checking File or Directory
PHP File: Showing All File in A Directory
PHP File: Showing All Files and Folders in Particular Directory
PHP File: Retrieving File Statistic
PHP File: Retrieving More File Information
PHP File: Counting Lines in a File
PHP File: Counting Characters and Words in File
PHP File: Writing File
PHP File: Locking and Unlocking File
PHP File: Removing Lines From File
PHP File: Inserting Lines into a File


Tag: file, directory, folder Category: PHP Basic Post : April 13th 2008 Read: 2,205 Bookmark and Share

blog comments powered by Disqus