ActionApps PHP Cross Reference Groupware Applications

Source: /misc/htmlarea/plugins/ImageManager/Classes/Files.php - 333 lines - 8311 bytes - Summary - Text - Print

Description: File Utilities.

   1  <?php
   2  /**
   3   * File Utilities.
   4   * @author $Author:koto $
   5   * @version $Id:Files.php 841 2007-05-27 13:31:51Z koto $
   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  define('FILE_ERROR_DST_DIR_EXIST', 104);
  14  
  15  /**
  16   * File Utilities
  17   * @author $Author:koto $
  18   * @version $Id:Files.php 841 2007-05-27 13:31:51Z koto $
  19   * @package ImageManager
  20   * @subpackage files
  21   */
  22  class Files 
  23  {
  24      
  25      /**
  26       * Copy a file from source to destination. If unique == true, then if
  27       * the destination exists, it will be renamed by appending an increamenting 
  28       * counting number.
  29       * @param string $source where the file is from, full path to the files required
  30       * @param string $destination_file name of the new file, just the filename
  31       * @param string $destination_dir where the files, just the destination dir,
  32       * e.g., /www/html/gallery/
  33       * @param boolean $unique create unique destination file if true.
  34       * @return string the new copied filename, else error if anything goes bad.
  35       */
  36  	function copyFile($source, $destination_dir, $destination_file, $unique=true) 
  37      {
  38          if(!(file_exists($source) && is_file($source))) 
  39              return FILE_ERROR_NO_SOURCE;
  40  
  41          $destination_dir = Files::fixPath($destination_dir);
  42  
  43          if(!is_dir($destination_dir)) 
  44              Return FILE_ERROR_DST_DIR_FAILED;
  45  
  46          $filename = Files::escape($destination_file);
  47  
  48          if($unique) 
  49          {
  50              $dotIndex = strrpos($destination_file, '.');
  51              $ext = '';
  52              if(is_int($dotIndex)) 
  53              {
  54                  $ext = substr($destination_file, $dotIndex);
  55                  $base = substr($destination_file, 0, $dotIndex);
  56              }
  57              $counter = 0;
  58              while(is_file($destination_dir.$filename)) 
  59              {
  60                  $counter++;
  61                  $filename = $base.'_'.$counter.$ext;
  62              }
  63          }
  64  
  65          if (!copy($source, $destination_dir.$filename))
  66              return FILE_ERROR_COPY_FAILED;
  67          
  68          //verify that it copied, new file must exists
  69          if (is_file($destination_dir.$filename))
  70              Return $filename;
  71          else
  72              return FILE_ERROR_COPY_FAILED;
  73      }
  74  
  75      /**
  76       * Create a new folder.
  77       * @param string $newFolder specifiy the full path of the new folder.
  78       * @return boolean true if the new folder is created, false otherwise.
  79       */
  80  	function createFolder($newFolder) 
  81      {
  82          mkdir ($newFolder, 0777);
  83          return chmod($newFolder, 0777);
  84      }
  85  
  86  
  87      /**
  88       * Escape the filenames, any non-word characters will be
  89       * replaced by an underscore.
  90       * @param string $filename the orginal filename
  91       * @return string the escaped safe filename
  92       */
  93  	function escape($filename)
  94      {
  95          Return preg_replace('/[^\w\._]/', '_', $filename);
  96      }
  97  
  98      /**
  99       * Delete a file.
 100       * @param string $file file to be deleted
 101       * @return boolean true if deleted, false otherwise.
 102       */
 103  	function delFile($file) 
 104      {
 105          if(is_file($file)) 
 106              Return unlink($file);
 107          else
 108              Return false;
 109      }
 110  
 111      /**
 112       * Delete folder(s), can delete recursively.
 113       * @param string $folder the folder to be deleted.
 114       * @param boolean $recursive if true, all files and sub-directories
 115       * are delete. If false, tries to delete the folder, can throw
 116       * error if the directory is not empty.
 117       * @return boolean true if deleted.
 118       */
 119  	function delFolder($folder, $recursive=false) 
 120      {
 121          $deleted = true;
 122          if($recursive) 
 123          {
 124              $d = dir($folder);
 125              while (false !== ($entry = $d->read())) 
 126              {
 127                  if ($entry != '.' && $entry != '..')
 128                  {
 129                      $obj = Files::fixPath($folder).$entry;
 130                      //var_dump($obj);
 131                      if (is_file($obj))
 132                      {
 133                          $deleted &= Files::delFile($obj);                    
 134                      }
 135                      else if(is_dir($obj))
 136                      {
 137                          $deleted &= Files::delFolder($obj, $recursive);
 138                      }
 139                      
 140                  }
 141              }
 142              $d->close();
 143  
 144          }
 145  
 146          //$folder= $folder.'/thumbs';
 147          //var_dump($folder);
 148          if(is_dir($folder)) 
 149              $deleted &= rmdir($folder);
 150          else
 151              $deleted &= false;
 152  
 153          Return $deleted;
 154      }
 155  
 156      /**
 157       * Append a / to the path if required.
 158       * @param string $path the path
 159       * @return string path with trailing /
 160       */
 161  	function fixPath($path) 
 162      {
 163          //append a slash to the path if it doesn't exists.
 164          if(!(substr($path,-1) == '/'))
 165              $path .= '/';
 166          Return $path;
 167      }
 168  
 169      /**
 170       * Concat two paths together. Basically $pathA+$pathB
 171       * @param string $pathA path one
 172       * @param string $pathB path two
 173       * @return string a trailing slash combinded path.
 174       */
 175  	function makePath($pathA, $pathB) 
 176      {
 177          $pathA = Files::fixPath($pathA);
 178          if(substr($pathB,0,1)=='/')
 179              $pathB = substr($pathB,1);
 180          Return Files::fixPath($pathA.$pathB);
 181      }
 182  
 183      /**
 184       * Similar to makePath, but the second parameter
 185       * is not only a path, it may contain say a file ending.
 186       * @param string $pathA the leading path
 187       * @param string $pathB the ending path with file
 188       * @return string combined file path.
 189       */
 190  	function makeFile($pathA, $pathB) 
 191      {        
 192          $pathA = Files::fixPath($pathA);
 193          if(substr($pathB,0,1)=='/')
 194              $pathB = substr($pathB,1);
 195          
 196          Return $pathA.$pathB;
 197      }
 198  
 199      
 200      /**
 201       * Format the file size, limits to Mb.
 202       * @param int $size the raw filesize
 203       * @return string formated file size.
 204       */
 205  	function formatSize($size) 
 206      {
 207          if($size < 1024) 
 208              return $size.' bytes';    
 209          else if($size >= 1024 && $size < 1024*1024) 
 210              return sprintf('%01.2f',$size/1024.0).' KB';
 211          else
 212              return sprintf('%01.2f',$size/(1024.0*1024)).' MB';
 213      }
 214  
 215      /**
 216       * Returns size of a directory, with all file & subdirectory
 217       * sizes added up
 218       * @param string dir path
 219       * @return int
 220       */
 221  	function dirSize($dirName = '.')
 222      {
 223          $dir  = dir($dirName);
 224          $size = 0;
 225  
 226          while ($file = $dir->read()) {
 227              if ($file != '.' && $file != '..')
 228              {
 229                  if (is_dir("$dirName$file"))
 230                  {
 231                      $size += Files::dirSize($dirName . '/' . $file);
 232                  }
 233                  else
 234                  {
 235                      $size += filesize($dirName . '/' . $file);
 236                  }
 237              }
 238          }
 239          $dir->close();
 240          return $size;
 241      }
 242      
 243      /**
 244       * Renames file, preserving its directory and extension
 245       * @param string $oldPath path to the old existing file
 246       * @param string new filename (just the name, without path or extension)
 247       * @author Krzysztof Kotowicz <koto@webworkers.pl>
 248       */
 249  	function renameFile($oldPath, $newName) {
 250  
 251          if(!(file_exists($oldPath) && is_file($oldPath)))
 252              return FILE_ERROR_NO_SOURCE;
 253  
 254          $oldFileParts = pathinfo($oldPath);
 255  
 256          $newPath = $oldFileParts['dirname'] . '/'
 257                     . $newName
 258                     . (!empty($oldFileParts['extension']) ? '.' . $oldFileParts['extension'] : '');
 259  
 260          if (file_exists($newPath))
 261              return false;
 262  
 263          if (!rename($oldPath, $newPath))
 264              return FILE_ERROR_COPY_FAILED;
 265  
 266      }
 267      
 268  	function rename ($oldPath,$newPath)
 269      {
 270          if(!(is_dir($oldPath) || is_file($oldPath)))
 271              return FILE_ERROR_NO_SOURCE;
 272          
 273          if (file_exists($newPath))
 274              return FILE_ERROR_DST_DIR_EXIST;
 275  
 276          $ret = rename($oldPath, $newPath);
 277          if (!$ret)
 278              return FILE_ERROR_COPY_FAILED;
 279          else return FILE_COPY_OK;
 280      }
 281      
 282      /**
 283       * copy a directory and all subdirectories and files (recursive)
 284       * @author SBoisvert at Don'tSpamMe dot Bryxal dot ca (adapted from php.net)
 285       * @author Raimund Meyer
 286       * @param string base path
 287       * @param string source directory
 288       * @param string destination directory
 289       * @param bool   overwrite existing files
 290       *  
 291       * @return mixed bool true on pass, number on fail
 292       */
 293    	function copyDir($basePath, $source, $dest, $overwrite = false)
 294      {
 295          if(!is_dir($basePath . $dest))
 296          {
 297              if (!@mkdir($basePath . $dest)) return FILE_ERROR_DST_DIR_FAILED;    
 298          }
 299          if($handle = opendir($basePath . $source))
 300          {        // if the folder exploration is sucsessful, continue
 301              while( ($file = readdir($handle)) !== false)
 302              { // as long as storing the next file to $file is successful, continue
 303                  if($file != '.' && $file != '..')
 304                  {
 305                      $path = $source . '/' . $file;
 306                      if(is_file($basePath . $path))
 307                      {
 308                          /*if(!is_file($basePath . $dest . '/' . $file) || $overwrite)
 309                          {
 310                              if(!@copy($basePath . $path, $basePath . $dest . '/' . $file))
 311                              {
 312                                return FILE_ERROR_COPY_FAILED;
 313                              }
 314                          }*/
 315                          Files::copyFile($basePath . $path, $basePath . $dest . '/', $file, true);
 316                      } 
 317                      elseif(is_dir($basePath . $path))
 318                      {
 319                          if(!is_dir($basePath . $dest . '/' . $file))
 320                          {
 321                              mkdir($basePath . $dest . '/' . $file); // make subdirectory before subdirectory is copied
 322                              Files::copyDir($basePath, $path, $dest . '/' . $file, $overwrite); //recurse!
 323                          }
 324                      }
 325                  }
 326              }
 327              closedir($handle);
 328          }
 329          return true;
 330      }
 331  }
 332  
 333  ?>

title

Description

title

Description

title

Description

title

title

Body