Directory handling methods.
<?php
/**
* @Class: CDirectory.class.php
* @Author: Abhishek Kumar
**/
require_once("CTemplate.class.php");
require_once("CFile.class.php");
class CDirectory
{
private $VoTemplate;
private $VoCFile;
private $Avoid;
public function __construct()
{
$this->VoTemplate = new CTemplate();
$this->VoCFile = new CFile();
$this->Avoid = array();
$this->Avoid['Directory'] = array();
$this->Avoid['File'] = array(".", "..");
$this->Avoid['Extension'] = array();
}
public function Directory($iCommand, $iDir)
{
switch($iCommand)
{
case 'CREATE':
mkdir($iDir, 0700);
$oStatus = true;
break;
case 'REMOVE':
rmdir($iDir);
$oStatus = true;
break;
case 'DELETE':
$this->deleteDir($iDir);
$oStatus = true;
break;
default:
$oStatus = false;
}
return $oStatus;
}
private function deleteDir($dir)
{
if (substr($dir, strlen($dir)-1, 1) != '/')
$dir .= '/';
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!$this->deleteDir($dir.$obj))
return false;
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
return false;
}
}
}
closedir($handle);
if (!@rmdir($dir))
return false;
return true;
}
return false;
}
public function setAvoid($Command, $List)
{
switch($Command)
{
case 'Directory':
$this->Avoid['Directory'] = $List;
break;
case 'File':
$this->Avoid['File'] = array_merge($this->Avoid['File'], $List);
break;
case 'Extension':
$this->Avoid['Extension'] = $List;
break;
}
}
public function Explore($iFolder, $iLevel)
{
$Directory = opendir("./$iFolder");
$TFolderArg = array();
$fileLevelPrefix = $folderLevelPrefix = "\n";
for($i=0; $i<$iLevel; $i++)
{
if($i < $iLevel-1)
{
$folderLevelPrefix .= "\t";
}
$fileLevelPrefix .= "\t";
}
while (false !== ($Entry = readdir($Directory)))
{
$fileExtension = $this->VoCFile->getFileExtensionAlt($Entry);
if($this->isPresent($Entry, $this->Avoid['File']) == 0 && $this->isPresent($fileExtension, $this->Avoid['Extension']) == 0)
{
if($fileExtension == NULL)
{
$DiveIn = $iFolder.'/'.$Entry;
if($this->isPresent($DiveIn, $this->Avoid['Directory']) == 0)
{
$TFolderArg['Content'] .= $this->Explore($DiveIn, $iLevel+1);
}
}
else
{
$TFileArg = array();
$TFileArg['filepath'] = $iFolder.'/'.$Entry;
$TFileArg['filetype'] = $fileExtension;
$TFileArg['filename'] = $this->VoCFile->getFileNameAlt($Entry);
$fileTemplate = $fileLevelPrefix.'';
$TFolderArg['Content'] .= $this->VoTemplate->FitIn($fileTemplate, $TFileArg);
}
}
}
closedir();
$TFolderArg['folderpath'] = ($iFolder == "") ? "/." : $iFolder;
$TFolderArg['foldername'] = $this->getFolderName($TFolderArg['folderpath']);
$TFolderArg['Content'] = ($TFolderArg['Content'] == NULL) ? "" : $TFolderArg['Content'];
$folderTemplate = $folderLevelPrefix.'[Content]'.$folderLevelPrefix.' ';
$Output = $this->VoTemplate->FitIn($folderTemplate, $TFolderArg);
return $Output;
}
private function isPresent($sItem, $aDatabase)
{
$nOutput = 0;
for ($i = 0; $i < count($aDatabase); $i++)
{
if ($aDatabase[$i] == $sItem)
{
$nOutput = 1;
}
}
return ($nOutput);
}
private function getFolderName($iPath)
{
$oPath = explode("/", $iPath);
return $oPath[count($oPath)-1];
}
}
/* EXAMPLE:
$handleDir = new CDirectory();
$handleDir->Directory('CREATE','e:/dirname');
$handleDir->Directory('DELETE','e:/dirname');
*/
?>
Comments
Post a Comment