Source for file Files.php

Documentation is available at Files.php

  1. <?php
  2. /**
  3.  * File Utilities.
  4.  * @author $Author$
  5.  * @version $Id$
  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$
  17.  * @version $Id$
  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.         if(!(file_exists($source&& is_file($source))) 
  38.             return FILE_ERROR_NO_SOURCE;
  39.  
  40.         $destination_dir Files::fixPath($destination_dir);
  41.  
  42.         if(!is_dir($destination_dir)) 
  43.             Return FILE_ERROR_DST_DIR_FAILED;
  44.  
  45.         $filename Files::escape($destination_file);
  46.  
  47.         if($unique
  48.         {
  49.             $dotIndex strrpos($destination_file'.');
  50.             $ext '';
  51.             if(is_int($dotIndex)) 
  52.             {
  53.                 $ext substr($destination_file$dotIndex);
  54.                 $base substr($destination_file0$dotIndex);
  55.             }
  56.             $counter 0;
  57.             while(is_file($destination_dir.$filename)) 
  58.             {
  59.                 $counter++;
  60.                 $filename $base.'_'.$counter.$ext;
  61.             }
  62.         }
  63.  
  64.         if (!copy($source$destination_dir.$filename))
  65.             return FILE_ERROR_COPY_FAILED;
  66.         
  67.         //verify that it copied, new file must exists
  68.         if (is_file($destination_dir.$filename))
  69.             Return $filename;
  70.         else
  71.             return FILE_ERROR_COPY_FAILED;
  72.     }
  73.  
  74.     /**
  75.      * Create a new folder.
  76.      * @param string $newFolder specifiy the full path of the new folder.
  77.      * @return boolean true if the new folder is created, false otherwise.
  78.      */
  79.     function createFolder($newFolder
  80.     {
  81.         mkdir ($newFolder0777);
  82.         return chmod($newFolder0777);
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Escape the filenames, any non-word characters will be
  88.      * replaced by an underscore.
  89.      * @param string $filename the orginal filename
  90.      * @return string the escaped safe filename
  91.      */
  92.     function escape($filename
  93.     {
  94.         Return preg_replace('/[^\w\._]/''_'$filename);
  95.     }
  96.  
  97.     /**
  98.      * Delete a file.
  99.      * @param string $file file to be deleted
  100.      * @return boolean true if deleted, false otherwise.
  101.      */
  102.     function delFile($file
  103.     {
  104.         if(is_file($file)) 
  105.             Return unlink($file);
  106.         else
  107.             Return false;
  108.     }
  109.  
  110.     /**
  111.      * Delete folder(s), can delete recursively.
  112.      * @param string $folder the folder to be deleted.
  113.      * @param boolean $recursive if true, all files and sub-directories
  114.      *  are delete. If false, tries to delete the folder, can throw
  115.      *  error if the directory is not empty.
  116.      * @return boolean true if deleted.
  117.      */
  118.     function delFolder($folder$recursive=false
  119.     {
  120.         $deleted true;
  121.         if($recursive
  122.         {
  123.             $d dir($folder);
  124.             while (false !== ($entry $d->read())) 
  125.             {
  126.                 if ($entry != '.' && $entry != '..')
  127.                 {
  128.                     $obj Files::fixPath($folder).$entry;
  129.                     //var_dump($obj);
  130.                     if (is_file($obj))
  131.                     {
  132.                         $deleted &= Files::delFile($obj);                    
  133.                     }
  134.                     else if(is_dir($obj))
  135.                     {
  136.                         $deleted &= Files::delFolder($obj$recursive);
  137.                     }
  138.                     
  139.                 }
  140.             }
  141.             $d->close();
  142.  
  143.         }
  144.  
  145.         //$folder= $folder.'/thumbs';
  146.         //var_dump($folder);
  147.         if(is_dir($folder)) 
  148.             $deleted &= rmdir($folder);
  149.         else
  150.             $deleted &= false;
  151.  
  152.         Return $deleted;
  153.     }
  154.  
  155.     /**
  156.      * Append a / to the path if required.
  157.      * @param string $path the path
  158.      * @return string path with trailing /
  159.      */
  160.     function fixPath($path
  161.     {
  162.         //append a slash to the path if it doesn't exists.
  163.         if(!(substr($path,-1== '/'))
  164.             $path .= '/';
  165.         Return $path;
  166.     }
  167.  
  168.     /**
  169.      * Concat two paths together. Basically $pathA+$pathB
  170.      * @param string $pathA path one
  171.      * @param string $pathB path two
  172.      * @return string a trailing slash combinded path.
  173.      */
  174.     function makePath($pathA$pathB
  175.     {
  176.         $pathA Files::fixPath($pathA);
  177.         if(substr($pathB,0,1)=='/')
  178.             $pathB substr($pathB,1);
  179.         Return Files::fixPath($pathA.$pathB);
  180.     }
  181.  
  182.     /**
  183.      * Similar to makePath, but the second parameter
  184.      * is not only a path, it may contain say a file ending.
  185.      * @param string $pathA the leading path
  186.      * @param string $pathB the ending path with file
  187.      * @return string combined file path.
  188.      */
  189.     function makeFile($pathA$pathB
  190.     {        
  191.         $pathA Files::fixPath($pathA);
  192.         if(substr($pathB,0,1)=='/')
  193.             $pathB substr($pathB,1);
  194.         
  195.         Return $pathA.$pathB;
  196.     }
  197.  
  198.     
  199.     /**
  200.      * Format the file size, limits to Mb.
  201.      * @param int $size the raw filesize
  202.      * @return string formated file size.
  203.      */
  204.     function formatSize($size
  205.     {
  206.         if($size 1024
  207.             return $size.' bytes';    
  208.         else if($size >= 1024 && $size 1024*1024
  209.             return sprintf('%01.2f',$size/1024.0).' Kb';    
  210.         else
  211.             return sprintf('%01.2f',$size/(1024.0*1024)).' Mb';    
  212.     }
  213. }
  214.  
  215. ?>

Documentation generated on Mon, 05 May 2008 16:19:43 +0400 by phpDocumentor 1.4.0