Recommended sites
Web Hosting Reviews

Creating a file system browser

In this tutorial I will show you how to create a PHP application to display directory content and navigate between them.

Step 1.
First of all we will create a basic html form. Here you can define the root directory in case of you want to display a directory content other than the actual. As html form creation is not part of this tutorial I just include the form code here without more comment:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="path">
        <table width="100%">
          <tr><td>Path: <input class="text" name="path" type="text" size="40" value="<?php echo $actpath; ?>" /></td></tr>
          <tr><td align="center"><br/><input class="text" type="submit" name="submitBtn" value="List content" /></td></tr>
        </table>  
      </form>
</body>


Step 2.
Now let's check the input value and set a valid root directory. Later on this preconfigured directory string will be passed to a separate function as parameter. The PHP code to setup the base directory is the following:

<?php
if (isset($_POST['submitBtn'])){
    
$actpath = isset($_POST['path']) ? $_POST['path'] : '.';    
} else {
    
$actpath = isset($_GET['path']) ? $_GET['path'] : '.';    
}
?>


Step 3.
In this step we will create a function to list all entries in the given directory. To do this PHP has a built in function called readdir().

<?php
function showContent($path){

   if (
$handle = opendir($path))
   {
       
// Iterate over all entries in the directory and display them
          
while (false !== ($file = readdir($handle)))
       {
           echo 
"<tr><td>".$file."</td></tr>";
       }

       
closedir($handle);
   }    
}
?>


Step 4.
To make the complete script to work we call our new function to write directory content as tags into a table element. So the complete script looks like this:

function showContent($path){

   if ($handle = opendir($path))
   {
       // Iterate over all entries in the directory and display them
          while (false !== ($file = readdir($handle)))
       {
           echo "<tr><td>".$file."</td></tr>";
       }

       closedir($handle);
   }    
}

if (isset($_POST['submitBtn'])){
    $actpath = isset($_POST['path']) ? $_POST['path'] : '.';    
} else {
    $actpath = isset($_GET['path']) ? $_GET['path'] : '.';    
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="path">
        <table width="100%">
          <tr><td>Path: <input class="text" name="path" type="text" size="40" value="<?php echo $actpath; ?>" /></td></tr>
          <tr><td align="center"><br/><input class="text" type="submit" name="submitBtn" value="List content" /></td></tr>
        </table>  
      </form><br/>

        <table width="100%">
<?php
            showContent
($actpath);        
?>
        </table>
</body>   


Step 5.
It works but not really nice. And at the moment it can list only the given directory and you can not navigate using links. Let's extend the function a little bit. We will display all item as link so you can go deep in a directory structure. To do this use the following code:

<?php
function showContent($path){

   if (
$handle = opendir($path))
   {
       
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
       echo 
"<tr><td>";
       echo 
"<a href='".$_SERVER['PHP_SELF']."?path=$up'>Up one level</a>";
       echo 
"</td></tr>";

       while (
false !== ($file = readdir($handle)))
       {
           if (
$file != "." && $file != "..")
           {
               
$fName  = $file;
               
$file   = $path.'/'.$file;
               
               if(
is_file($file)) {
                   echo 
"<tr><td> -F- <a href='".$file."'>".$fName."</a></td></tr>"
               
} elseif (is_dir($file)) {
                   print 
"<tr><td> -D- <a href='".$_SERVER['PHP_SELF']."?path=$file'>$fName</a></td></tr>";
               }
           }
       }

       
closedir($handle);
   }    

}
?>


Step 6.
The result is much better. However not complete yet. In general the file size and the modification date can be an interesting information as well. To get this informations we will use again some built in PHP functions as filemtime() and filesize().
The full function with extras is below.


<?php
function showContent($path){

   if (
$handle = opendir($path))
   {
       
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
       echo 
"<tr><td colspan='3'>";
       echo 
"<a href='".$_SERVER['PHP_SELF']."?path=$up'>Up one level</a>";
       echo 
"</td></tr>";

       while (
false !== ($file = readdir($handle)))
       {
           if (
$file != "." && $file != "..")
           {
               
$fName  = $file;
               
$file   = $path.'/'.$file;
               
               if(
is_file($file)) {
                   echo 
"<tr><td> -F- <a href='".$file."'>".$fName."</a></td>"
                            
."<td align='right'>".date ('d-m-Y H:i:s', filemtime($file))."</td>"
                            
."<td align='right'>".filesize($file)." bytes</td></tr>";
               } elseif (
is_dir($file)) {
                   print 
"<tr><td colspan='3'> -D- <a href='".$_SERVER['PHP_SELF']."?path=$file'>$fName</a></td></tr>";
               }
           }
       }

       
closedir($handle);
   }    

}
?>

Sponsored links