My PHP Directory Listing Script

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
rrinc
-


Joined: 24 Feb 2006
Posts: 725
Location: Arkansas, USA

PostPosted: Sat Aug 04, 2007 5:28 pm    Post subject: My PHP Directory Listing Script Reply with quote

I thought I would post my directory listing script, it isn't sortable, but it pretty simple and provides file sizes and distinctions between files/folders.

Code:
<?php /* Directory Listing */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <title><?php echo str_replace('http://','',''.$_SERVER['SCRIPT_URI']); ?></title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
   <style type="text/css">
   a:link, a:visited, a:active {color:red;}
   body {font-size:90%;font-family:verdana; }
   </style>
</head>
<body>
<!-- HEADER HTML -->
<span style="font-size: 175%;"><?php echo str_replace('http://','',''.$_SERVER['SCRIPT_URI']); ?></span><br /><hr />
<table border=0 style="font-size:90%;">
<tr><td><u><b>File</b></u></td><td><u><b>Size</b></u></td><td><u><b>Type</b></u></td></tr>
<!-- END OF HEADER HTML -->

<!-- DIRECTORY LISTING: -->
<?php
strpos(strtolower(PHP_OS),'win') ? $path = $_SERVER['DOCUMENT_ROOT'].str_replace('index.php','',str_replace('/','\\',$_SERVER["REQUEST_URI"])) :
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$dir_handle = @opendir($path) or die("Cannot read folder contents.<br>");
while ($file = readdir($dir_handle))
{
  if($file!='.' && $file!='..' && $file!='index.php')
  {
   if (is_file($file)) { $type = 'File'; }
   else { $type = 'Folder'; }
   if ($type == 'File')
   {
      $size = filesize($file);
      if ($size >= 1024)
      {$size = $size / 1024; $unit = 'KB';}
      if ($size >= 1024)
      {$size = $size / 1024; $unit = 'MB';}
      $size = round($size).' '.$unit;
   }
   
   
   echo "<tr><td style=\"padding-right:15px;\"><a href=\"$file\">$file</a></td><td>$size</td><td>$type</td></tr>";
  }
}
closedir($dir_handle);
?></table>
<!-- END OF DIRECTORY LISTING -->

<!-- FOOTER HTML -->
<hr />

</body>
</html>

It's nothing awesome, but I made it awhile back when playing with directories and files. I might make a better version some time.

Do you think I chose a good way to get the folder's path? It should work on Windows or Linux (if you notice, I have a small check for the OS...I haven't tested it yet).
_________________
-Blake | New Server :D
SaveTheInternet
Soy hispanohablante. Puedes contactarme por mensajes privados.
Back to top View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Aug 09, 2007 12:54 am    Post subject: Re: My PHP Directory Listing Script Reply with quote

rrinc wrote:
Code:
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];



This line makes it only work when the URL does not refer to an alias. That's by the way why we've created the whole directory listing scripts concept: it was to make it easy to display listings without dealing with the nasty details of directories, aliases, and location mappings (since the server collects the file information for you).
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group