Advertisement
fruffl

Directory Stuff

Jan 12th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.73 KB | None | 0 0
  1. <?PHP
  2.  
  3.     FINAL CLASS ILLI_Container_Api_Directory EXTENDS ILLI_Container
  4.     {
  5.         /**
  6.          * dir-cache
  7.          *
  8.          * save parsed directories
  9.          *
  10.          * @var array
  11.          */
  12.         private $dircache = array();
  13.         private $sizecache = array();
  14.        
  15.         const DIRECTORIES_ONLY = '.';
  16.    
  17.         /**
  18.          * create directories from array
  19.          *
  20.          * @param array array with dir-paths
  21.          * @param bool $exception enable throw-exception
  22.          * @throws ILLI_Container_Api_Directory_Exception when directory does't exists
  23.          * @throws ILLI_Container_Api_Directory_Exception when directory can not create
  24.          */
  25.         public function create(array $stack, $exception = FALSE)
  26.         {
  27.             foreach($stack as $key => $value)
  28.                 if(!is_dir($value))
  29.                 {
  30.                     if($exception)
  31.                     {
  32.                         throw new ILLI_Container_Api_Directory_Exception
  33.                         (
  34.                             sprintf
  35.                             (
  36.                                 ILLI_Exception_Interface::ERR_DIR_NOT_FOUND,
  37.                                 $value
  38.                             )
  39.                         );
  40.                            
  41.                         return FALSE;
  42.                     }
  43.                    
  44.                    
  45.                     if(!mkdir($value, 755, TRUE))
  46.                     {
  47.                         throw new ILLI_Container_Api_Directory_Exception
  48.                         (
  49.                             sprintf
  50.                             (
  51.                                 ILLI_Exception_Interface::ERR_DIR_CAN_NOT_CREATE,
  52.                                 $value
  53.                             )
  54.                         );
  55.                            
  56.                            
  57.                         return FALSE;
  58.                     }
  59.                 }
  60.                        
  61.             return $this;
  62.         }
  63.        
  64.         public function read($path)
  65.         {
  66.             return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
  67.         }
  68.        
  69.        
  70.         public function unlinkFilesOf($dirpath, $filetype = NULL)
  71.         {
  72.             $files = array();
  73.             $queue = $this->contentOf($dirpath, $filetype);
  74.             if(is_array($dirpath))
  75.             {
  76.                 if(is_array($queue))
  77.                     foreach($queue as $p => $dir)
  78.                         if(is_array($dir))
  79.                             foreach($dir as $f => $file)
  80.                                 $files[] = $file->getRealPath();
  81.             }
  82.             else
  83.             {
  84.                 if(is_array($queue))
  85.                     foreach($queue as $f => $file)
  86.                         $files[] = $file->getRealPath();
  87.             }
  88.            
  89.            
  90.             foreach($files as $trash)
  91.                 @unlink($trash);
  92.         }
  93.        
  94.         public function sizeOf($dirpath, $filetype = NULL, $secure = FALSE)
  95.         {
  96.             $size = 0;
  97.             $queue = $this->contentOf($dirpath, $filetype, $secure);
  98.            
  99.             if(is_array($dirpath))
  100.             {
  101.                 if(is_array($queue))
  102.                     foreach($queue as $p => $dir)
  103.                         if(is_array($dir))
  104.                             foreach($dir as $f => $file)
  105.                                 $size += $file->getSize();
  106.             }
  107.             else
  108.             {
  109.                 if(is_array($queue))
  110.                     foreach($queue as $f => $file)
  111.                         $size += $file->getSize();
  112.             }
  113.        
  114.             return $size;
  115.         }
  116.        
  117.         public function countFiles($dirpath, $filetype = NULL, $secure = FALSE)
  118.         {
  119.             $count = 0;
  120.             $queue = $this->contentOf($dirpath, $filetype, $secure);
  121.            
  122.             if(is_array($dirpath))
  123.             {
  124.                 if(is_array($queue))
  125.                     foreach($queue as $p => $dir)
  126.                         if(is_array($dir))
  127.                             foreach($dir as $f => $file)
  128.                                 $count++;
  129.             }
  130.             else
  131.             {
  132.                 if(is_array($queue))
  133.                     foreach($queue as $f => $file)
  134.                         $count++;
  135.             }
  136.        
  137.             return $count;
  138.         }
  139.        
  140.         /**
  141.          * directory-content as array with SplFileInfo Objects
  142.          *
  143.          * @param $dirpath string   path of dir
  144.          * @param $filetype string  filter files by extension
  145.          * @param $secure bool will parse mime-type
  146.          * @throws ILLI_Container_Api_Directory_Exception when file/mimetype is not readable
  147.          * @return array SplFileInfo-objects-array filtered by filtype
  148.          */
  149.         public function contentOf($dirpath, $filetype = NULL, $secure = FALSE)
  150.         {
  151.            
  152.             $result = array();
  153.            
  154.             if(is_string($dirpath))
  155.             {
  156.                 if(!is_dir($dirpath))
  157.                     return FALSE;
  158.                    
  159.                 if(array_key_exists($dirpath, $this->dircache))
  160.                     return $this->dircache[$dirpath];
  161.                    
  162.                 foreach(new RecursiveDirectoryIterator($dirpath) as $file_obj)
  163.                 {
  164.                     if(NULL === $filetype)
  165.                     {
  166.                         $result[$file_obj->getFilename()] = $file_obj;
  167.                     }
  168.                     else
  169.                     {
  170.                         if(is_string($filetype))
  171.                         {
  172.                             if($filetype == self::DIRECTORIES_ONLY)
  173.                             {
  174.                                 if(is_dir($file_obj->getPathname()))
  175.                                     $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
  176.                             }
  177.                             else
  178.                             if(strtolower($filetype) == strtolower(pathinfo($file_obj->getFilename(), PATHINFO_EXTENSION)))
  179.                                 $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
  180.                         }
  181.                         else
  182.                         if(is_array($filetype))
  183.                         {
  184.                             foreach($filetype as $i => $type)
  185.                                 if(strtolower($type) == strtolower(pathinfo($file_obj->getFilename(), PATHINFO_EXTENSION)))
  186.                                     $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
  187.                         }
  188.                     }
  189.                    
  190.                    
  191.                    
  192.                     if($secure)
  193.                     {
  194.                         if(!function_exists('stream_get_meta_data'))
  195.                             throw new ILLI_Container_Api_Directory_Exception('Unable to use function ["stream_get_meta_data"]: not implemented.');
  196.                            
  197.                             if(is_dir($dirpath.DIRECTORY_SEPARATOR.$file_obj->getFilename()))
  198.                                 continue;
  199.                                
  200.                         $parts = stream_get_meta_data($hook = fopen($file = $dirpath.DIRECTORY_SEPARATOR.$file_obj->getFilename(), "r"));
  201.                        
  202.                         if($hook && $parts)
  203.                         {
  204.                             fclose($hook);
  205.                                
  206.                             $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME)).'_META'] = $parts;
  207.                         }
  208.                         else
  209.                         {
  210.                             throw new ILLI_Container_Api_Directory_Exception
  211.                             (
  212.                                 sprintf
  213.                                 (
  214.                                     ILLI_Exception_Interface::ERR_FILE_METADATA_ERROR,
  215.                                     $file
  216.                                 )
  217.                             );
  218.                         }
  219.                     }
  220.                    
  221.                 }
  222.                
  223.                 $this->dircache[$dirpath] = $result;
  224.             }
  225.             else
  226.             if(is_array($dirpath))
  227.             {
  228.                 foreach($dirpath as $path)
  229.                 {
  230.                     $isset = $this->contentOf($path, $filetype, $secure);
  231.                     if(FALSE === $isset)
  232.                         continue;
  233.                     $result[$path] = $isset;
  234.                 }
  235.             }
  236.            
  237.             return $result;
  238.         }
  239.        
  240.        
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement