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:
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:
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().
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:
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:
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.
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:
Code:
<!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>
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:
Code:
<?php if (isset($_POST['submitBtn'])){ $actpath = isset($_POST['path']) ? $_POST['path'] : '.'; } else { $actpath = isset($_GET['path']) ? $_GET['path'] : '.'; } ?>
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().
Code:
<?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); } } ?>
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:
Code:
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>
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:
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); } } ?>
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.
Code:
<?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); } } ?>