<?php
 
 
/**
 
 * Class used to simulate basic directory operations.
 
 * 
 
 * @author Marius Zadara <[email protected]>
 
 * @copyright (C) Marius Zadara <[email protected]>
 
 * @final 
 
 */
 
final class MyDirectory
 
{
 
    /**
 
     * Absolute path to directory
 
     * @var string 
 
     */
 
    private $path;
 
    
 
    
 
    /**
 
     * Constructor of the class - used to set the path of the directory
 
     * If the path (absolute) is not specified, then the default path (current) is used
 
     *
 
     * @param string $path The absolute path of the directory
 
     * @return Directory
 
     */
 
    public function MyDirectory($path = ".")
 
    {
 
        // set the class member
 
        $this->path = $path;                
 
    }
 
 
    /**
 
     * This function is used to validate the current directory
 
     * 
 
     * @throws DirectoryException
 
     * @return TRUE In case the path is valid
 
     * @return FALSE in case the path is not valid
 
     */
 
    public function validate($validEmpty = false)
 
    {
 
        // the resource must exists
 
        if (!file_exists($this->path))
 
            throw new DirectoryException(sprintf("The resource '%s' does not exists.", $this->path));
 
            
 
        // the resource must be a directory
 
        if (!is_dir($this->path))
 
            throw new DirectoryException(sprintf("The resource '%s' is not a directory.", $this->path));
 
            
 
        // the resource must be readable
 
        if (!is_readable($this->path))
 
            throw new DirectoryException(sprintf("The directory '%s' is not readable.", $this->path));
 
            
 
        // try to access the directory
 
        $dirHandle = @opendir($this->path);
 
 
        if (!$dirHandle)
 
            throw new DirectoryException(sprintf("The directory '%s' could not be accessed", $this->path));
 
                        
 
        // at this point, the directory is valid
 
        // according to the parameter, check the content
 
        
 
        // files count
 
        $filesCount = 0;
 
        
 
        // read each record from the directory
 
        while (($file = readdir($dirHandle)) !== false)
 
        {
 
            // skip the current directory and parent directory
 
            if (($file == ".") || ($file == ".."))
 
                continue;
 
            
 
            // make the file full path
 
            $fileFullPath = $this->path . DIRECTORY_SEPARATOR . $file;    
 
                                
 
            // skip all the non-files
 
            if (filetype($fileFullPath) != "file")
 
                continue;
 
                
 
            // skip all the empty files
 
            if (filesize($fileFullPath) == 0)
 
                continue;                                
 
                
 
            // if reached this point, a file has been found
 
            // continue the loop until we have at least 2 files for comparison
 
            $filesCount ++;
 
            
 
            // break the loop when more than 2 files
 
            if ($filesCount >= 2)
 
                break;
 
        }                
 
        
 
        // check the files count
 
        if ($filesCount == 0)
 
        {
 
            @closedir($dirHandle);
 
            throw new DirectoryException(sprintf("The directory '%s' does not contain any valid files.", $this->path));
 
        }
 
            
 
        // need at least 2 files for comparison
 
        if ($filesCount < 2)
 
        {
 
            @closedir($dirHandle);
 
            throw new DirectoryException(sprintf("The directory '%s' must have at least 2 files for comparison.", $this->path));
 
        }
 
            
 
        // at this point, everything seems ok
 
        // close the directory handle and return true
 
        
 
        @closedir($dirHandle);
 
        return true;            
 
    }
 
 
 
    /**
 
     * Function used to create a directory using the given path
 
     * 
 
     * @return boolean TRUE if the directory has been created
 
     * @throws DirectoryException If error occured during creation
 
     *
 
     */
 
    public function create()
 
    {
 
        // try to create the destination directory
 
        // if not already exists
 
        if (!file_exists($this->path))
 
        {
 
            if (!mkdir($this->path))
 
                throw new DirectoryException(sprintf("The path '%s' could not be created", $this->path));
 
        }
 
        
 
        // make sure that the directory has the persmissions 
 
        // to write and execute data (everything for the owner, read and execute for the others)
 
        if (!@chmod($this->path, 0777))
 
            throw new DirectoryException(sprintf("Could not set the attributes on the path '%s'.", $this->path));
 
        
 
        // everything is ok at this point
 
        return true;
 
    }
 
 
    /*
 
     * This function is used in order to read the content of the directory
 
     * @throws DirectoryException If the directory could not be opened to read the content
 
     */
 
    public function getContent()
 
    {
 
        // try to access the directory
 
        $dirHandle = @opendir($this->path);
 
 
        if (!$dirHandle)
 
            throw new DirectoryException(sprintf("The directory '%s' could not be accessed", $this->path));
 
        
 
        // init the files list
 
        $dirFiles = array();    
 
            
 
        // read each record from the directory
 
        while (($file = readdir($dirHandle)) !== false)
 
        {
 
            // skip the current directory and parent directory
 
            if (($file == ".") || ($file == ".."))
 
                continue;
 
 
            // make the file fullpath
 
            $fileFullPath = $this->path . DIRECTORY_SEPARATOR . $file;    
 
                                
 
            // skip all the non-files
 
            if (filetype($fileFullPath) != "file")
 
                continue;
 
                
 
            // skip all the empty files
 
            if (filesize($fileFullPath) == 0)
 
                continue;                                
 
                                
 
            // add the curent file to the list as the full path using the directory path
 
            $dirFiles[] = $this->path . DIRECTORY_SEPARATOR . $file;     
 
        }
 
        
 
        // check the files list
 
        if (sizeof($dirFiles) == 0)
 
            throw new DirectoryException(sprintf("Could not find any files to add from the directory '%s'.", $this->path));
 
            
 
        // return the built list
 
        return $dirFiles;
 
    }
 
}
 
 
 
?>
 
 |