<html>
<head>
<title>Directory Listing</title>
<style>
th {color:blue}
</style>
</head>
<body bgcolor=lightyellow>

<?php

//===============================================================================

    error_reporting(0);                         //turn off PHP error reporting

    $dir = $_GET['dir'];                        //get requested directory
        
    if (! $dir) $dir = '.';                     //if no directory, assume current dir

    print "<h2>Directory Listing -<br>";
    print "<font size=+1>".realpath($dir)."</font></h2>";
    print "<hr>";

    $dirHandle  = opendir($dir);                //get handle to current directory

    while($file = readdir($dirHandle))          //while there are files
    {
        if ($file == '.')                       //skip for current directory
            continue;           

        $fileArray[] = $file;                   //build an array of files
    }

    closedir($dirHandle);                       //close handle to directory                             
         
    sort($fileArray);                           //sort array of files 

    print "<table style='background-color:cccccc'>";
    print "<tr bgcolor=tan><th width=200>Name<th width=100>Size<th width=50><th>Last Modified<th>Permissions";
        
    foreach($fileArray as $file)                                        //for each file
    {
        $fullpath = $dir."/".$file;                                     //create a full path
    
        print "<tr><td>";

        if ($file == '..') $file = "Parent Directory";                  //parent directory 
 
        if (is_dir($fullpath))                                          //regular directory 
            print "<img src=folder.gif>
                   <a href=directory.php?dir=$fullpath>$file</a><br />\n";

        elseif (is_file($fullpath))                                     //regular file  
            print "<img src=file.gif>
                   <a href=$fullpath>$file</a><br />\n";

        print "<td align=right><b>" . filesize($fullpath) . "</b> Bytes";
        print "<td>  ";

        $lastModified = filemtime($fullpath);  
        print "<td> " . date("l, dS F, Y @ h:ia", $lastModified);       //a timestamp

        $perm_octal = fileperms($fullpath);                     //an octal number
        $string     = sprintf('%o', $perm_octal);               //convert to string
        $perm       = substr($string, -3);                      //substr starting from the end
        print  "<td align=center> $perm";                       
    }

    print "</table>";
    print "<hr>";
?>

<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->
</body>
</html>