Files::copyFile PHP Method

copyFile() public method

Copy a file from source to destination. If unique == true, then if the destination exists, it will be renamed by appending an increamenting counting number.
public copyFile ( string $source, string $destination_dir, string $destination_file, boolean $unique = true ) : string
$source string where the file is from, full path to the files required
$destination_dir string where the files, just the destination dir, e.g., /www/html/gallery/
$destination_file string name of the new file, just the filename
$unique boolean create unique destination file if true.
return string the new copied filename, else error if anything goes bad.
    function copyFile($source, $destination_dir, $destination_file, $unique = true)
    {
        if (!(file_exists($source) && is_file($source))) {
            return FILE_ERROR_NO_SOURCE;
        }
        $destination_dir = Files::fixPath($destination_dir);
        if (!is_dir($destination_dir)) {
            return FILE_ERROR_DST_DIR_FAILED;
        }
        $filename = Files::escape($destination_file);
        if ($unique) {
            $dotIndex = strrpos($destination_file, '.');
            $ext = '';
            if (is_int($dotIndex)) {
                $ext = substr($destination_file, $dotIndex);
                $base = substr($destination_file, 0, $dotIndex);
            }
            $counter = 0;
            while (is_file($destination_dir . $filename)) {
                $counter++;
                $filename = $base . '_' . $counter . $ext;
            }
        }
        if (!copy($source, $destination_dir . $filename)) {
            return FILE_ERROR_COPY_FAILED;
        }
        //verify that it copied, new file must exists
        if (is_file($destination_dir . $filename)) {
            return $filename;
        } else {
            return FILE_ERROR_COPY_FAILED;
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Process upload files. The file must be an 
  * uploaded file. If 'validate_images' is set to
  * true, only images will be processed. Any duplicate
  * file will be renamed. See Files::copyFile for details
  * on renaming.
  * @param string $relative the relative path where the file
  * should be copied to.
  * @param array $file the uploaded file from $_FILES
  * @return boolean true if the file was processed successfully, 
  * false otherwise
  */
 function _processFiles($relative, $file)
 {
     if ($file['error'] != 0) {
         return false;
     }
     if (!is_file($file['tmp_name'])) {
         return false;
     }
     if (!is_uploaded_file($file['tmp_name'])) {
         Files::delFile($file['tmp_name']);
         return false;
     }
     if ($this->config['validate_images'] == true) {
         $imgInfo = @getImageSize($file['tmp_name']);
         if (!is_array($imgInfo)) {
             Files::delFile($file['tmp_name']);
             return false;
         }
     }
     $valid_extensions = $this->config['allowed_image_extensions'];
     $afruext = strtolower(substr(strrchr($file['name'], "."), 1));
     if (!in_array($afruext, $valid_extensions)) {
         Files::delFile($file['tmp_name']);
         return 'Cannot upload $extension=' . $afruext . '$ Files. Permission denied.';
     }
     //now copy the file
     $path = Files::makePath($this->getImagesDir(), $relative);
     $result = Files::copyFile($file['tmp_name'], $path, $file['name']);
     //no copy error
     if (!is_int($result)) {
         Files::delFile($file['tmp_name']);
         return true;
     }
     //delete tmp files.
     Files::delFile($file['tmp_name']);
     return false;
 }
All Usage Examples Of Files::copyFile