ImageManager
[ class tree: ImageManager ] [ index: ImageManager ] [ all elements ]

Source for file Files.php

Documentation is available at Files.php

  1. <?php
  2. /**
  3.  * File Utilities.
  4.  * @author $Author: Wei Zhuo $
  5.  * @version $Id: Files.php 26 2004-03-31 02:35:21Z Wei Zhuo $
  6.  * @package ImageManager
  7.  */
  8.  
  9. define('FILE_ERROR_NO_SOURCE'100);
  10. define('FILE_ERROR_COPY_FAILED'101);
  11. define('FILE_ERROR_DST_DIR_FAILED'102);
  12. define('FILE_COPY_OK'103);
  13.  
  14. /**
  15.  * File Utilities
  16.  * @author $Author: Wei Zhuo $
  17.  * @version $Id: Files.php 26 2004-03-31 02:35:21Z Wei Zhuo $
  18.  * @package ImageManager
  19.  * @subpackage files
  20.  */
  21. class Files 
  22. {
  23.     
  24.     /**
  25.      * Copy a file from source to destination. If unique == true, then if
  26.      * the destination exists, it will be renamed by appending an increamenting
  27.      * counting number.
  28.      * @param string $source where the file is from, full path to the files required
  29.      * @param string $destination_file name of the new file, just the filename
  30.      * @param string $destination_dir where the files, just the destination dir,
  31.      *  e.g., /www/html/gallery/
  32.      * @param boolean $unique create unique destination file if true.
  33.      * @return string the new copied filename, else error if anything goes bad.
  34.      */
  35.     function copyFile($source$destination_dir$destination_file$unique=true
  36.     {
  37.         
  38.         $destination_file=utf8_decode($destination_file);
  39.         $destination_filestrtr($destination_file,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn");
  40.         $destination_file=Files::escape($destination_file);
  41.         
  42.         if(!(file_exists($source&& is_file($source))) 
  43.             return FILE_ERROR_NO_SOURCE;
  44.  
  45.         $destination_dir Files::fixPath($destination_dir);
  46.  
  47.         if(!is_dir($destination_dir)) 
  48.             Return FILE_ERROR_DST_DIR_FAILED;
  49.  
  50.         $filename Files::escape($destination_file);
  51.  
  52.         if($unique
  53.         {
  54.             $dotIndex strrpos($destination_file'.');
  55.             $ext '';
  56.             if(is_int($dotIndex)) 
  57.             {
  58.                 $ext substr($destination_file$dotIndex);
  59.                 $base substr($destination_file0$dotIndex);
  60.             }
  61.             $counter 0;
  62.             while(is_file($destination_dir.$filename)) 
  63.             {
  64.                 $counter++;
  65.                 $filename $base.'_'.$counter.$ext;
  66.             }
  67.         }
  68.  
  69.         if (!copy($source$destination_dir.$filename))
  70.             return FILE_ERROR_COPY_FAILED;
  71.         
  72.         //verify that it copied, new file must exists
  73.         if (is_file($destination_dir.$filename))
  74.             Return $filename;
  75.         else
  76.             return FILE_ERROR_COPY_FAILED;
  77.     }
  78.  
  79.     /**
  80.      * Create a new folder.
  81.      * @param string $newFolder specifiy the full path of the new folder.
  82.      * @return boolean true if the new folder is created, false otherwise.
  83.      */
  84.     function createFolder($newFolder
  85.     {
  86.         $perm api_get_setting('permissions_for_new_directories');
  87.         $perm octdec(!empty($perm)?$perm:'0770');
  88.         mkdir ($newFolder$perm);
  89.         return chmod($newFolder$perm);
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Escape the filenames, any non-word characters will be
  95.      * replaced by an underscore.
  96.      * @param string $filename the orginal filename
  97.      * @return string the escaped safe filename
  98.      */
  99.     function escape($filename
  100.     {
  101.         Return preg_replace('/[^\w\._]/''_'$filename);
  102.     }
  103.  
  104.     /**
  105.      * Delete a file.
  106.      * @param string $file file to be deleted
  107.      * @return boolean true if deleted, false otherwise.
  108.      */
  109.     function delFile($file
  110.     {
  111.         if(is_file($file)) 
  112.             Return unlink($file);
  113.         else
  114.             Return false;
  115.     }
  116.  
  117.     /**
  118.      * Delete folder(s), can delete recursively.
  119.      * @param string $folder the folder to be deleted.
  120.      * @param boolean $recursive if true, all files and sub-directories
  121.      *  are delete. If false, tries to delete the folder, can throw
  122.      *  error if the directory is not empty.
  123.      * @return boolean true if deleted.
  124.      */
  125.     function delFolder($folder$recursive=false
  126.     {
  127.         $deleted true;
  128.         if($recursive
  129.         {
  130.             $d dir($folder);
  131.             while (false !== ($entry $d->read())) 
  132.             {
  133.                 if ($entry != '.' && $entry != '..')
  134.                 {
  135.                     $obj Files::fixPath($folder).$entry;
  136.                     //var_dump($obj);
  137.                     if (is_file($obj))
  138.                     {
  139.                         $deleted &= Files::delFile($obj);                    
  140.                     }
  141.                     else if(is_dir($obj))
  142.                     {
  143.                         $deleted &= Files::delFolder($obj$recursive);
  144.                     }
  145.                     
  146.                 }
  147.             }
  148.             $d->close();
  149.  
  150.         }
  151.  
  152.         //$folder= $folder.'/thumbs';
  153.         //var_dump($folder);
  154.         if(is_dir($folder)) 
  155.             $deleted &= rmdir($folder);
  156.         else
  157.             $deleted &= false;
  158.  
  159.         Return $deleted;
  160.     }
  161.  
  162.     /**
  163.      * Append a / to the path if required.
  164.      * @param string $path the path
  165.      * @return string path with trailing /
  166.      */
  167.     function fixPath($path
  168.     {
  169.         //append a slash to the path if it doesn't exists.
  170.         if(!(substr($path,-1== '/'))
  171.             $path .= '/';
  172.         Return $path;
  173.     }
  174.  
  175.     /**
  176.      * Concat two paths together. Basically $pathA+$pathB
  177.      * @param string $pathA path one
  178.      * @param string $pathB path two
  179.      * @return string a trailing slash combinded path.
  180.      */
  181.     function makePath($pathA$pathB
  182.     {
  183.         $pathA Files::fixPath($pathA);
  184.         if(substr($pathB,0,1)=='/')
  185.             $pathB substr($pathB,1);
  186.         Return Files::fixPath($pathA.$pathB);
  187.     }
  188.  
  189.     /**
  190.      * Similar to makePath, but the second parameter
  191.      * is not only a path, it may contain say a file ending.
  192.      * @param string $pathA the leading path
  193.      * @param string $pathB the ending path with file
  194.      * @return string combined file path.
  195.      */
  196.     function makeFile($pathA$pathB
  197.     {        
  198.         $pathA Files::fixPath($pathA);
  199.         if(substr($pathB,0,1)=='/')
  200.             $pathB substr($pathB,1);
  201.         
  202.         Return $pathA.$pathB;
  203.     }
  204.  
  205.     
  206.     /**
  207.      * Format the file size, limits to Mb.
  208.      * @param int $size the raw filesize
  209.      * @return string formated file size.
  210.      */
  211.     function formatSize($size
  212.     {
  213.         if($size 1024
  214.             return $size.' bytes';    
  215.         else if($size >= 1024 && $size 1024*1024
  216.             return sprintf('%01.2f',$size/1024.0).' Kb';    
  217.         else
  218.             return sprintf('%01.2f',$size/(1024.0*1024)).' Mb';    
  219.     }
  220. }
  221.  
  222. ?>

Documentation generated on Thu, 12 Jun 2008 13:29:02 -0500 by phpDocumentor 1.4.1