PHP Directory List

By Jonesy44 on Apr 24, 2008

Ok, so this short php script is real usefull for if you have loads of subdirectories of completely different websites like i do. these can be webs for sale, or just random websites shrugs it's for your use :-)

Check the example pic for more details (upload after i post this)
But the basics are, change the $dir variable to the directory you want to search including "/home/www/" if you are on a general apache server (You can find this by simple editing a file in the generic Browser Control Panel, it will say what file you are editing, therefore giving the directory. Once you point the script towards the main directory, it will search any subdirectories, if it finds an "index.php" file within a subdirectory, it will give a link to it, and more info, else it will message an error message.
(Check back to example pic)

Where it says "$dir = THE_DIRECTORY_GOES_HERE;", it will be something like "/home/www/wfs.myartsonline.com/" which is what i use in the picture example.

Drop a comment if you need some help or have any queries :-) or simply a comment.

For a general example of this: http://wfs.myartsonline.com


Add this stylsheet if you want it (COMPLETELY OPTIONAL!)

<style>
body {
 font-family: times;
}
h1 {
 font-family: times;
}
a {
 color: #000000;
 background: #ffffff;
}
a:hover {
 color: #ffffff;
 background: #000000;
}
</style>

Example:
Image

<?php

//  Directory Listing  //
//  Script by Jonesy44 //
//    www.hawkee.com   //

$dir = THE_DIRECTORY_GOES_HERE;
// Edit me ^^ ! //
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if (fnmatch("*.php", $file)) {
      echo '';
    }
    elseif (fnmatch(".*", $file)) {
      echo '';
    }
    else {
      $indfile = '' .$file. '/index.php';
      if (!file_exists($indfile)) {
        echo '>> ' .$file. '<br>';
        echo '<font color=grey style="font-size:11px;"> <b>Error:</b> No "index.php" file exists within this directory</font><br>';
      }
      else {
        echo '>> <a href=/' .$file. '>' .$file. '</a><br>';
        $size = round(filesize($indfile) / 1024, 2);
        $mod = filemtime($indfile);
        echo '<font color=grey style="font-size:11px;">File Size: ' .$size. 'KB; Last Modified: ' .date('jS F Y [g:i A]', $mod). ';</font><br>';
      }
    }
  }
  closedir($handle);
}

?>

Comments

Sign in to comment.
Hawkee   -  Aug 25, 2008

Could probably better organize your if-statements. You don't really need the first two echos. Maybe something like,

if(!fnmatch("*.php", $file) and !fnmatch(".*", $file)) 
{
$indfile = '' .$file. '/index.php';
...
 Respond  
Jonesy44   -  Jun 16, 2008

I\'ve used preg_match since.
But since the server i run on is CentOS, i\'m all good :p

 Respond  
markusPHP   -  Jun 16, 2008

http://uk.php.net/fnmatch
Scroll down for some talk on fnmatch() substitutes

 Respond  
Jonesy44   -  May 05, 2008

:-s

Any ideas on what i can use instead of fnmatch ?

 Respond  
F*U*R*B*Y*   -  May 05, 2008

This will not work on a Windows Operating System, as PHP on Windows does not contain the fnmatch function....

 Respond  
Jonesy44   -  Apr 24, 2008

*Update, added the example image :P

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.