1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
<?php
class DirectoryHandler extends ErrorHandler
{
var $__DIR_HANDLER = null;
var $__DIR_NAME = "";
var $__TO_HIDE = null;
function openDirectory()
{
if($this->__DIR_NAME == "")
{
$this->setError("You must provide a directory name");
return false;
}
if(!is_dir($this->__DIR_NAME))
{
$this->setError($this->__DIR_NAME." is not a directory");
return false;
}
$this->__DIR_HANDLER = @opendir($this->__DIR_NAME);
$this->__DIR_NAME = $DIRNAME;
if(!$this->__DIR_HANDLER)
{
$this->setError("Could not open directory '".$this->__DIR_NAME."'");
return false;
}
return true;
}
function closeDirectory()
{
closedir($this->__DIR_HANDLER);
$this->__DIR_HANDLER = null;
$this->__DIR_NAME = "";
}
function readDirectory($DIRNAME)
{
$this->__DIR_NAME = $DIRNAME;
if(!$this->openDirectory())
return false;
$directories = array();
$files = array();
while(false !== ($file = readdir($this->__DIR_HANDLER)))
{
$allow = 1;
if(is_array($this->__TO_HIDE))
{
foreach($this->__TO_HIDE AS $k=>$v)
{
if($v == ".")
$v = "\.";
$pregString = "/^$v/";
if(preg_match($pregString,$file))
$allow = 0;
}
}
if($allow)
{
if(is_dir($DIRNAME."/".$file))
array_push($directories,$file);
if(is_file($DIRNAME."/".$file))
array_push($files,$file);
}
}
$this->closeDirectory();
sort($files);
sort($directories);
return array_merge($directories,$files);
}
function hideFiles($FILES=".|CVS")
{ $this->__TO_HIDE = explode("|",$FILES);}
}
?>
|