lampware banner lampware banner lampware banner
Home FAQ Careers Clients Utilities Downloads
lsdir

<html>
 <head>
  <!-- © 2002-2007 lampware, inc.
       Written by kemiko for lampware, inc. in PHX.AZ.USA
       This script will list a directory to look like a Linux ls,
       but you can sort the columns and click to open the files.
       The footer is total file size and average date.
    -->

  <title>
   Directory Listing
  </title>
  <style type="text/css">
<!--
   a:link    {color: #000000;text-decoration:none}
   a:active  {color: #000000;text-decoration:none}
   a:visited {color: #000000;text-decoration:none}
   pre       {font-size: 90%}
   body      {font-family: courier, helvetica, verdana, sans-serif}
  -->
  </style>
 </head>
<?php
  printf( " <body bgcolor=\"%s\">\n", "#DDC69A" );

  // Remove the following "if" statement and contents if upload is not desired!
  if( $_SERVER["REMOTE_USER"] == "admin" )
  {
    $MAX_SIZE = 25000000;
    $FILE_MIMES = array(
                         'image/gif',
                         'image/jpg',
                         'image/png',
                         'text/plain',
                         'text/html',
                         'application/pdf',
                         'application/excel',
                         'application/msword',
                         'application/octet-stream'
                       );
    $FILE_EXTS = array(
                        '.gif',
                        '.jpg',
                        '.png',
                        '.txt',
                        '.htm',
                        '.php',
                        '.gz',
                        '.tgz',
                        '.mp3',
                        '.zip',
                        '.pdf',
                        '.xls',
                        '.doc',
                        '.reg'
                      );

    $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_dir = "./";
    $upload_url = $url_dir."/";
    $message = "";
  
    function do_upload( $upload_dir, $upload_url )
    {
      $temp_name = $_FILES['userfile']['tmp_name'];
      $file_name = $_FILES['userfile']['name'];
      $file_name = str_replace( "\\", "", $file_name );
      $file_name = str_replace( "'", "", $file_name );
      $file_path = $upload_dir . $file_name;
  
      if( $file_name == "" )
      {
        $message = "Invalid File Name Specified!";
        return $message;
      }
  
      $result = move_uploaded_file( $temp_name, $file_path );

      if( !$result )
      {
        $message = "\"$file_name\" was NOT uploaded successfully.";
      }
      else if( !chmod( $file_path, 0664 ) )
      {
        $message = "Change permission to 664 failed.";
      }
      else if( $result )
      {
        $message = "\"$file_name\" was uploaded successfully.";
      }
      else
      {
        $message = "Somthing is wrong with uploading the file.";
      }
  
      return $message;
    }
  
    if( $_FILES['userfile'] )
    {
      $file_type = $_FILES['userfile']['type'];
      $file_name = $_FILES['userfile']['name'];
      $file_ext = strtolower( substr( $file_name, strrpos( $file_name, "." ) ) );
  
      if( $_FILES['userfile']['size'] > $MAX_SIZE )
      {
         $message = sprintf( "\"$file_name\" is over %dMB.", $MAX_SIZE / 1000000 );
      }
      else if( !in_array( $file_type, $FILE_MIMES ) && !in_array( $file_ext, $FILE_EXTS ) )
      {
         $message = "Sorry, \"$file_name\" ($file_type) is not allowed to be uploaded.";
      }
      else
      {
         $message = do_upload( $upload_dir, $upload_url );
      }
    }
  }
  
  $dir = "./";
  
  // Open a known directory, and proceed to read its contents
  if( is_dir( $dir ) )
  {
    if( $dh = opendir( $dir ) )
    {
       $count = 0;
       while( ( $file = readdir( $dh ) ) !== false )
       {
         $perms = fileperms( $dir . $file );
  
         if( ( $perms & 0xC000 ) == 0xC000 )
         {
           $info = 's'; // Socket
         }
         elseif( ( $perms & 0xA000 ) == 0xA000 )
         {
           $info = 'l'; // Symbolic Link
         }
         elseif( ( $perms & 0x8000 ) == 0x8000 )
         {
           $info = '-'; // Regular
         }
         elseif( ( $perms & 0x6000 ) == 0x6000 )
         {
           $info = 'b'; // Block special
         }
         elseif( ( $perms & 0x4000 ) == 0x4000 )
         {
           $info = 'd'; // Directory
         }
         elseif( ( $perms & 0x2000 ) == 0x2000 )
         {
           $info = 'c'; // Character special
         }
         elseif( ( $perms & 0x1000 ) == 0x1000 )
         {
           $info = 'p'; // FIFO pipe
         }
         else
         {
           $info = 'u'; // Unknown
         }
  
         // Owner
         $info .= ( ( $perms & 0x0100 ) ? 'r' : '-' );
         $info .= ( ( $perms & 0x0080 ) ? 'w' : '-' );
         $info .= ( ( $perms & 0x0040 ) ?
                  ( ( $perms & 0x0800 ) ? 's' : 'x' ) :
                  ( ( $perms & 0x0800 ) ? 'S' : '-' ) );
  
         // Group
         $info .= ( ( $perms & 0x0020 ) ? 'r' : '-' );
         $info .= ( ( $perms & 0x0010 ) ? 'w' : '-' );
         $info .= ( ( $perms & 0x0008 ) ?
                  ( ( $perms & 0x0400 ) ? 's' : 'x' ) :
                  ( ( $perms & 0x0400 ) ? 'S' : '-' ) );
  
         // World
         $info .= ( ( $perms & 0x0004 ) ? 'r' : '-' );
         $info .= ( ( $perms & 0x0002 ) ? 'w' : '-' );
         $info .= ( ( $perms & 0x0001 ) ?
                  ( ( $perms & 0x0200 ) ? 't' : 'x' ) :
                  ( ( $perms & 0x0200 ) ? 'T' : '-' ) );
  
         $owner_number = fileowner( $dir . $file );
         $owner_array = posix_getpwuid( $owner_number );
         if( $owner_array["name"] )
         {
           $owner_show = $owner_array["name"];
         }
         else
         {
           $owner_show = $owner_number;
         }

         $group_number = filegroup( $dir . $file );
         $group_array = posix_getgrgid( $group_number );
         if( $group_array["name"] )
         {
           $group_show = $group_array["name"];
         }
         else
         {
           $group_show = $group_number;
         }

         $size = filesize( $dir . $file );
         $date = filemtime( $dir . $file );
  
         $record = array( "Permission" => "$info", 
                          "Owner" => "$owner_show",
                          "Group" => "$group_show",
                          "Size" => $size,
                          "Date" => "$date",
                          "File" => "$file" );
         $records[$count] = $record;
  
         $count += 1;
       }
  
      if( $_GET["field"] )
      {
        $sort_field = $_GET["field"]; // Enter the number of field to sort
      }
      else
      {
        $sort_field = "File"; // Enter the number of field to sort
      }
      if( $_GET["order"] )
      {
        $sort_order = $_GET["order"];  // Enter the order of fields to sort
      }
      else
      {
        $sort_order = 'ASC';  // Enter the order of fields to sort
      }
  
      // Compare function alpha
      function cmpa( $a, $b )
      {
        global $sort_field;
        global $sort_order;
        if( $sort_order == "ASC" )
        {
          return strcmp( $a[$sort_field], $b[$sort_field] );
        }
        else
        {
          return strcmp( $b[$sort_field], $a[$sort_field] );
        }
      }
  
      // Compare function numeric
      function cmpn( $a, $b )
      {
        global $sort_field;
        global $sort_order;
        if( $sort_order == "ASC" )
        {
          if( $a[$sort_field] < $b[$sort_field] )
          {
            return 0;
          }
          else
          {
            return 1;
          }
        }
        else
        {
          if( $a[$sort_field] > $b[$sort_field] )
          {
            return 0;
          }
          else
          {
            return 1;
          }
        }
      }
  
      // Do the array sorting
      if( $sort_field == "Date" || $sort_field == "Size" )
      {
        usort( $records, 'cmpn' );
      }
      else
      {
        usort( $records, 'cmpa' );
      }
  
      // Start output
      print( "  <pre>\n" );
      print( "   <table border=\"0\">\n" );
      print( "    <tr>\n" );
      print( "     <td colspan=\"8\">\n" );
  
      $path = getenv( "SCRIPT_NAME" );
  
      $directories = array( "/" );
  
      for( $i = 1, $j = 1; $i < strlen( $path ); $i++ )
      {
        if( $path[$i] == "/" )
        {
          array_push( $directories, $directory );
  
          $directory = NULL;
          $j++;
        }
        $directory .= $path[$i];
      }
  
      print( "        <font size=\"+2\"><b>" );
      for( $i = 0; $directories[$i]; $i++ )
      {
        $output .= $directories[$i];
        printf( "<a href=\"%s\">%s</a>", $output, $directories[$i] );
      }
  
      print( "</b></font>\n" );
  
      print( "     </td>\n" );
      print( "    </tr>\n" );
  
  
      print( "    <tr>\n" );
      print( "     <td colspan=\"8\">\n" );
      if( $message )
      {
        printf( "        <font color=\"#990000\"><b>%s</b></font>\n", $message );
      }
      else
      {
        print( "       \n" );
      }
      print( "     </td>\n" );
      print( "    </tr>\n" );
  
      print( "    <tr>\n" );

      if( $_GET["field"] == "Permission" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Permission&order=DESC'\">\n" );
          print( "        <b>Permission</b> <img src=\"/graphics/up.gif\">\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Permission&order=ASC'\">\n" );
          print( "        <b>Permission</b> <img src=\"/graphics/dn.gif\">\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Permission&order=ASC'\">\n" );
        print( "        <b>Permission</b>\n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Owner" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Owner&order=DESC'\">\n" );
          print( "      <b>Owner</b> <img src=\"/graphics/up.gif\">\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Owner&order=ASC'\">\n" );
          print( "      <b>Owner</b> <img src=\"/graphics/dn.gif\">\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Owner&order=ASC'\">\n" );
        print( "      <b>Owner</b>\n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Group" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Group&order=DESC'\">\n" );
          print( "      <b>Group</b> <img src=\"/graphics/up.gif\">\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Group&order=ASC'\">\n" );
          print( "      <b>Group</b> <img src=\"/graphics/dn.gif\">\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Group&order=ASC'\">\n" );
        print( "      <b>Group</b>\n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Size" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td align=\"right\" style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Size&order=DESC'\">\n" );
          print( "      <b>Size</b> <img src=\"/graphics/up.gif\"> \n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td align=\"right\" style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Size&order=ASC'\">\n" );
          print( "      <b>Size</b> <img src=\"/graphics/dn.gif\"> \n" );
        }
      }
      else
      {
        print( "     <td align=\"right\" style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Size&order=ASC'\">\n" );
        print( "      <b>Size </b>\n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Date" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Date&order=DESC'\">\n" );
          print( "      <b>Date</b>\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Date&order=ASC'\">\n" );
          print( "      <b>Date</b>\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Date&order=ASC'\">\n" );
        print( "      <b>Date</b>\n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Date" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td align=\"left\" style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Date&order=DESC'\">\n" );
          print( "      <img src=\"/graphics/up.gif\">\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td align=\"left\" style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=Date&order=ASC'\">\n" );
          print( "      <img src=\"/graphics/dn.gif\">\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Date&order=ASC'\">\n" );
        print( "       \n" );
      }
      print( "     </td>\n" );

      if( $_GET["field"] == "Date" && $_GET["order"] == "ASC" )
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Date&order=DESC'\">\n" );
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=Date&order=ASC'\">\n" );
      }
      print( "       \n" );
      print( "     </td>\n" );

      if( $_GET["field"] == "File" )
      {
        if( $_GET["order"] == "ASC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=File&order=DESC'\">\n" );
          print( "      <b>Filename</b> <img src=\"/graphics/up.gif\">\n" );
        }
        if( $_GET["order"] == "DESC" )
        {
          print( "     <td style=\"cursor:pointer;cursor:hand\"
                       onclick=\"window.location.href='index.php?field=File&order=ASC'\">\n" );
          print( "      <b>Filename</b> <img src=\"/graphics/dn.gif\">\n" );
        }
      }
      else
      {
        print( "     <td style=\"cursor:pointer;cursor:hand\"
                     onclick=\"window.location.href='index.php?field=File&order=ASC'\">\n" );
        print( "      <b>Filename</b>\n" );
      }
      print( "     </td>\n" );
      print( "    </tr>\n" );
  
      for( $i = 0; $records[$i]; $i++ )
      {
        if( $records[$i]["File"] != "index.php" && !eregi( '^\.[a-zA-Z0-9]+', $records[$i]["File"] ) )
        {
          print( "    <tr>\n" );
          print( "     <td>\n" );
          printf( "      <a href=\"%s\">  %s </a>\n", $records[$i]["File"], $records[$i]["Permission"] );
          print( "     </td>\n" );
          print( "     <td>\n" );
          printf( "      <a href=\"%s\">%s </a>\n", $records[$i]["File"], $records[$i]["Owner"] );
          print( "     </td>\n" );
          print( "     <td>\n" );
          printf( "      <a href=\"%s\">%s </a>\n", $records[$i]["File"], $records[$i]["Group"] );
          print( "     </td>\n" );
          print( "     <td align=\"right\">\n" );
          $formatted = sprintf( "%' 10s", $records[$i]["Size"] );
          $formatted = str_replace( " ", " ", $formatted );
          printf( "      <a href=\"%s\">%s </a>\n", $records[$i]["File"], $formatted );
          print( "     </td>\n" );
          print( "     <td>\n" );
          printf( "      <a href=\"%s\">%s</a>\n", $records[$i]["File"], date( "M", $records[$i]["Date"] ) );
          print( "     </td>\n" );
          print( "     <td align=\"right\">\n" );
          printf( "      <a href=\"%s\">%s</a>\n", $records[$i]["File"], date( "j", $records[$i]["Date"] ) );
          print( "     </td>\n" );
          print( "     <td align=\"right\">\n" );
          if( date( "Y", time( NULL ) ) == date( "Y", $records[$i]["Date"] ) )
          {
            printf( "      <a href=\"%s\">%s </a>\n", $records[$i]["File"],
                           date( "H:i", $records[$i]["Date"] ) );
          }
          else
          {
            printf( "      <a href=\"%s\">%s </a>\n", $records[$i]["File"],
                           date( "Y", $records[$i]["Date"] ) );
          }
          print( "     </td>\n" );
          print( "     <td>\n" );
          printf( "      <a href=\"%s\">%s</a>\n", $records[$i]["File"], $records[$i]["File"] );
          print( "     </td>\n" );
          print( "    </tr>\n" );
        }
        $SIZE += $records[$i]["Size"];
        $DATE += $records[$i]["Date"];
      }
      // Size is a total of the directory
      $SIZE = sprintf( "%' 10s", $SIZE );
      $SIZE = str_replace( " ", " ", $SIZE );
      // Date is an average of the directory
      $DATE = $DATE / $i;
      $DATE = round( $DATE, 0 );
      print( "    <tr>\n" );
      print( "     <td>\n" );
      print( "       \n" );
      print( "     </td>\n" );
      print( "     <td>\n" );
      print( "       \n" );
      print( "     </td>\n" );
      print( "     <td>\n" );
      print( "       \n" );
      print( "     </td>\n" );
      print( "     <td>\n" );
      printf( "      %s\n", $SIZE );
      print( "     </td>\n" );
      print( "     <td>\n" );
      printf( "      %s\n", date( "M", $DATE ) );
      print( "     </td>\n" );
      print( "     <td align=\"right\">\n" );
      printf( "      %s\n", date( "j", $DATE ) );
      print( "     </td>\n" );
      print( "     <td align=\"right\">\n" );
      if( date( "Y", time( NULL ) ) == date( "Y", $DATE ) )
      {
        printf( "      %s \n", date( "H:i", $DATE ) );
      }
      else
      {
        printf( "      %s \n", date( "Y", $DATE ) );
      }
      print( "     </td>\n" );
      print( "     <td>\n" );
      print( "       \n" );
      print( "     </td>\n" );
      print( "    </tr>\n" );
      // Remove the following 12 lines if upload is not desired!
      if( $_SERVER["REMOTE_USER"] == "admin" )
      {
        print( "    <tr>\n" );
        print( "     <td>\n" );
        print( "       \n" );
        print( "     </td>\n" );
        print( "    </tr>\n" );
        print( "    <tr>\n" );
        print( "     <td colspan=\"8\">\n" );
        print( "      <form name=\"upload\" id=\"upload\" enctype=\"multipart/form-data\"
                      method=\"post\">\n" );
        print( "         <font size=\"+1\" color=\"#990000\"><b>Upload File:</b></font><input 
                         type=\"file\" id=\"userfile\" name=\"userfile\" style=\"height:22\"><input
                         type=\"submit\" name=\"upload\" value=\"Upload\" style=\"height:22\">\n" );
        print( "      </FORM>\n" );
        print( "     </td>\n" );
        print( "    </tr>\n" );
      }
      print( "   </table>\n" );
      print( "  </pre>\n" );
  
      closedir( $dh );
    }
  }
 ?>
 </body>
</html>

© 2002-2015 lampware.us