Gdn_Upload::saveAs PHP Method

saveAs() public method

public saveAs ( $Source, $Target, array $Options = [] ) : array | boolean
$Source
$Target
$Options array
return array | boolean
    public function saveAs($Source, $Target, $Options = array())
    {
        $this->EventArguments['Path'] = $Source;
        $Parsed = self::parse($Target);
        $this->EventArguments['Parsed'] =& $Parsed;
        $this->EventArguments['Options'] = $Options;
        $this->EventArguments['OriginalFilename'] = val('OriginalFilename', $Options);
        $Handled = false;
        $this->EventArguments['Handled'] =& $Handled;
        $this->fireAs('Gdn_Upload')->fireEvent('SaveAs');
        // Check to see if the event handled the save.
        if (!$Handled) {
            $Target = PATH_UPLOADS . '/' . $Parsed['Name'];
            if (!file_exists(dirname($Target))) {
                mkdir(dirname($Target));
            }
            if (stringBeginsWith($Source, PATH_UPLOADS)) {
                rename($Source, $Target);
            } elseif (!move_uploaded_file($Source, $Target)) {
                throw new Exception(sprintf(t('Failed to move uploaded file to target destination (%s).'), $Target));
            }
        }
        return $Parsed;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Create and display a thumbnail of an uploaded file.
  */
 public function utilityController_mediaThumbnail_create($sender, $media_id)
 {
     // When it makes it into core, it will be available in
     // functions.general.php
     require 'generate_thumbnail.php';
     $model = new Gdn_Model('Media');
     $media = $model->getID($media_id, DATASET_TYPE_ARRAY);
     if (!$media) {
         throw notFoundException('File');
     }
     // Get actual path to the file.
     $local_path = Gdn_Upload::copyLocal($media['Path']);
     if (!file_exists($local_path)) {
         throw notFoundException('File');
     }
     $file_extension = pathinfo($local_path, PATHINFO_EXTENSION);
     // Generate new path for thumbnail
     $thumb_path = $this->getBaseUploadDestinationDir() . '/' . 'thumb';
     // Grab full path with filename, and validate it.
     $thumb_destination_path = $this->getAbsoluteDestinationFilePath($local_path, $file_extension, $thumb_path);
     // Create thumbnail, and grab debug data from whole process.
     $thumb_payload = generate_thumbnail($local_path, $thumb_destination_path, array('height' => c('Plugins.FileUpload.ThumbnailHeight', 128)));
     if ($thumb_payload['success'] === true) {
         // Thumbnail dimensions
         $thumb_height = round($thumb_payload['result_height']);
         $thumb_width = round($thumb_payload['result_width']);
         // Move the thumbnail to its proper location. Calling SaveAs with
         // cloudfiles enabled will trigger the move to cloudfiles, so use
         // same path for each arg in SaveAs. The file will be removed from the local filesystem.
         $parsed = Gdn_Upload::parse($thumb_destination_path);
         $target = $thumb_destination_path;
         // $parsed['Name'];
         $Upload = new Gdn_Upload();
         $filepath_parsed = $Upload->saveAs($thumb_destination_path, $target, array('source' => 'content'));
         // Save thumbnail information to DB.
         $model->save(array('MediaID' => $media_id, 'StorageMethod' => $filepath_parsed['Type'], 'ThumbWidth' => $thumb_width, 'ThumbHeight' => $thumb_height, 'ThumbPath' => $filepath_parsed['SaveName']));
         // Remove cf scratch copy, typically in cftemp, if there was actually a file pulled in from CF.
         if (strpos($local_path, 'cftemp') !== false) {
             if (!unlink($local_path)) {
                 // Maybe add logging for local cf copies not deleted.
             }
         }
         $url = $filepath_parsed['Url'];
     } else {
         // Fix the thumbnail information so this isn't requested again and again.
         $model->save(array('MediaID' => $media_id, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => ''));
         $url = asset('/plugins/FileUpload/images/file.png');
     }
     redirect($url, 301);
 }
All Usage Examples Of Gdn_Upload::saveAs