示例#1
0
 /**
  * Creates a new file object with a copy of this file
  * 
  * If no directory is specified, the file is created with a new name in
  * the current directory. If a new directory is specified, you must also
  * indicate if you wish to overwrite an existing file with the same name
  * in the new directory or create a unique name.
  * 
  * This operation will be reverted by a filesystem transaction being rolled
  * back.
  * 
  * @param  string|fDirectory $new_directory  The directory to duplicate the file into if different than the current directory
  * @param  boolean           $overwrite      If a new directory is specified, this indicates if a file with the same name should be overwritten.
  * @return fFile  The new fFile object
  */
 public function duplicate($new_directory = NULL, $overwrite = NULL)
 {
     $this->tossIfDeleted();
     if ($new_directory === NULL) {
         $new_directory = $this->getParent();
     }
     if (!is_object($new_directory)) {
         $new_directory = new fDirectory($new_directory);
     }
     $new_filename = $new_directory->getPath() . $this->getName();
     $check_dir_permissions = FALSE;
     if (file_exists($new_filename)) {
         if (!$overwrite) {
             $new_filename = fFilesystem::makeUniqueName($new_filename);
             $check_dir_permissions = TRUE;
         } elseif (!is_writable($new_filename)) {
             throw new fEnvironmentException('The new directory specified, %1$s, already contains a file with the name %2$s, but it is not writable', $new_directory->getPath(), $this->getName());
         }
     } else {
         $check_dir_permissions = TRUE;
     }
     if ($check_dir_permissions) {
         if (!$new_directory->isWritable()) {
             throw new fEnvironmentException('The new directory specified, %s, is not writable', $new_directory);
         }
     }
     copy($this->getPath(), $new_filename);
     chmod($new_filename, fileperms($this->getPath()));
     $class = get_class($this);
     $file = new $class($new_filename);
     // Allow filesystem transactions
     if (fFilesystem::isInsideTransaction()) {
         fFilesystem::recordDuplicate($file);
     }
     return $file;
 }