Sunday, August 07, 2005

Directory index

Some (free) webservers do not allow to list files in directories. This script can help (thanks to http://www.plebian.com/news.php?artc=8):

<?php
unset($dir);
if(!empty($HTTP_GET_VARS['dir'])&&!strpos($HTTP_GET_VARS['dir'],'..')&&!strpos($HTTP_GET_VARS['dir'],'\.\.'))
{

$dir=$HTTP_SERVER_VARS['DOCUMENT_ROOT'].'/'.$HTTP_GET_VARS['dir'];
$d=&$HTTP_GET_VARS['dir'];
}
else
{
$dir='.';
$d='.';
}
clearstatcache();
if($a=@opendir($dir))
{
$dir=htmlspecialchars($dir);
$d=htmlspecialchars($d);
while(false!==($b=readdir($a)))
{
if(ereg("^\.",$b))
continue; // ignore hidden files
if ($b=='index.php') continue; //ignore index.php
echo (is_dir($b))?"
  • $b":"
  • $b";
    }
    closedir($a);
    }
    else
    {
    echo 'unable to open directory';
    }
    ?>