Creating a file system browser

Started on the old forum in February 2010 · view the original in the archive · old links redirect here

No replies · 802 views · started 16 years ago · 1 min read

riderz Lead · 975 posts #1

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

}
?>

Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close