Blog

gestion fichier et repertoire en php

  : Ajouté le 18/8/2009 à 15:52

class File
{


    private $rootPath ;
    private $content ;
    private $fileName;
    private    $fileContent;
    private $fileHandle;
    private $contentDirFile = array();
    private $dirSeparator = '/';

    public function __construct(    )
    {
        $this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned on');

    }

    /**
     *
     * @return unknown_type
     */
    public function getPath (  $rootPath )
    {
         $this->rootPath = $rootPath ;
        $this->content = $this -> createContent ($this -> rootPath );
    }

    public function __destruct()
    {
        $this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned off');
    }

    public function __get($mod)
    {
        return Mp::getInstance()->getMod($mod);
    }


    /**
     * method creating array containing the directories and files
     * @param String $dir contain rootPath name
     * @return Array return array containing the directories and files
     */
    private function createContent( $dir )
    {
        $dir = rtrim ($dir, '/');
         if (is_dir ($dir)) {
         $dh = opendir ($dir);
         } else
        {
             echo $dir, ' n\'est pas un repertoire valide';
             exit;
        }
         while (($file = readdir ($dh)) !== false ) {
             if ($file !== '.' && $file !== '..') {
                 $rootPath =$dir.'/'.$file;
                 if (is_dir ($rootPath)) {

                     $this->contentDirFile[$dir]['dir'][] = $rootPath;
                     $tabTmp = $this -> createContent ($rootPath);
                     if (is_array ($tabTmp) && is_array ($this->contentDirFile))
                     $this->contentDirFile = array_merge ($this->contentDirFile, $tabTmp);
                 }
                 else
                     $this->contentDirFile[$dir]['file'][] = $rootPath;
             }
         }
         closedir ($dh); // on ferme le repertoire courant
         return $this->contentDirFile;
    }

    /**
     *    method creating array containing the directories
     * @return Array return array directories
     */
    public function recursiveDisplayDir( )
    {
        if ( is_array( $this->content[$this -> rootPath]['dir'] )){
            return $this->content[$this -> rootPath]['dir'];
        }

    }
    /**
     *    method creating array containing files
     * @return Array return array files
     */
    public function recursiveDisplayFile (  )
    {
        if (is_array ($this->content[$this -> rootPath]['file'])) {

            return $this->content[$this -> rootPath]['file'];
        }
    }

    /**
     * method creating a directory
     * @param String $dirName contain directory name
     * @param Int $mode contain mode permission
     * @return bool return true on success
     */
    public function createDirectory( $dirName , $mode = 0755 )
    {
        $slash = '/';
        (stristr( $this->rootPath, $slash )) ? '' : $slash = '\\';
        if ( !empty($dirName) && !is_dir($dirName) ){
            $dirPath = $this->rootPath.$slash.$dirName;
            if( mkdir ( $dirPath, $mode )){

                return true ;
            } else {
                return false ;
            }
        }
    }

    /**
     * method deleting a directory
     * @param String $dirName contain directory name
     * @return bool return true on success
     */
    public function DeleteDirectory( $dirName )
    {

        $dirPath = $this->rootPath.$this->dirSeparator.$dirName.$this->dirSeparator ;

        if ( is_dir ( $dirPath )){
        $fileList = glob( $dirPath . '*', GLOB_MARK );
        foreach( $fileList as $file ){
            if( substr( $file, -1 ) == '/' ){
                $this->DeleteDirectory( $file );
            }else{
                unlink( $file );
            }
        }
        rmdir( $dirPath );
        }
    }

    /**
     * method creaing in file
     * @param String $fileName contain a resouce
     * @param String $mode contain a mode permission
     * @return bool return true on success
     */
    public function createFile( $fileName , $mode)
    {
        $this->fileName = $this->rootPath.$this->dirSeparator.$fileName;
        $this->fileHandle = fopen( $this->fileName, "$mode");
        if ( $this->fileHandle ){
            return true ;
        }else{
            return false ;
        }

    }

        /**
     * method reading in file
     * @param String $fileName contain a resouce
     * @return String return file content
     */
    public function readFile( $fileName )
    {
        $fileName = $this->rootPath.$this->dirSeparator.$fileName;
        if ( file_exists ( $fileName ) && is_readable( $fileName ) ){
            $this->fileHandle = fopen( $fileName , "r" );
            $this->fileContent = fread( $this->fileHandle , filesize($fileName) );
            $this->closeFile( $this->fileHandle );
        }

        return $this->fileContent ;
    }

    /**
     * method writing in file
     * @param String $fileName contain a resouce
     * @param String $fileContent contain a resouce
     * @return bool return true on success
     */
    public function writeFile( $fileName, $fileContent )
    {
        $file = $this->rootPath.$this->dirSeparator.$fileName;
        if ( file_exists ( $file ) && is_writable( $file )  ){

            $handle = fopen( "$file" , "a" );
            fwrite( $handle, $fileContent );
            $this->closeFile( $handle  );
        }else{

            return false ;
        }

    }

    /**
     * method deleting a directory
     * @param String $fileHandle contain a resouce
     * @return bool return true on success
     */
    public function closeFile( $fileHandle )
    {
        fclose( $fileHandle );
    }


}


/******************* utlisation ********************************/

<?php

$te = dirname(__FILE__)  ; // pour creation fichier

    $test =  new File();
    $test->getPath ( $te );

print"<pre>";
    //print_r( $test->recursiveDisplayFile() );
    //print_r( $test->recursiveDisplayDir());
    //print_r( $test->createFile("tes222t.php","a+") );
    //print_r( $test->DeleteDirectory("test2") );
    //print_r( $test->writeFile("tes222t.php","testetttfgtuhftt") );
    //print_r( $test->readFile("tes222t.php") );
    print_r( $test->createDirectory("test2", 0700) );
?>

Tags : file system php fichier dossier recursive php



» Catégorie divers
Commentaires (1) :: Poster un commentaire :: Lien permanent :: Envoyer à un ami
Créer un blog | Liens : Fonds d'écran gratuits | Katie Melua |  Contacter l'auteur