コード例 #1
0
 /**
  * Save an file passed from a form post into this object.
  * File names are filtered through {@link FileNameFilter}, see class documentation
  * on how to influence this behaviour.
  *
  * @param $tmpFile array Indexed array that PHP generated for every file it uploads.
  * @param $folderPath string Folder path relative to /assets
  * @return Boolean|string Either success or error-message.
  */
 public function load($tmpFile, $folderPath = false)
 {
     if (!is_array($tmpFile)) {
         throw new InvalidArgumentException("Upload::load() Not passed an array.  Most likely, the form hasn't got the right enctype");
     }
     // Validate
     $this->clearErrors();
     $valid = $this->validate($tmpFile);
     if (!$valid) {
         return false;
     }
     // Clean filename
     if (!$folderPath) {
         $folderPath = $this->config()->uploads_folder;
     }
     $nameFilter = FileNameFilter::create();
     $file = $nameFilter->filter($tmpFile['name']);
     $filename = basename($file);
     if ($folderPath) {
         $filename = File::join_paths($folderPath, $filename);
     }
     // Validate filename
     $filename = $this->resolveExistingFile($filename);
     // Save file into backend
     $conflictResolution = $this->replaceFile ? AssetStore::CONFLICT_OVERWRITE : AssetStore::CONFLICT_RENAME;
     $this->file->setFromLocalFile($tmpFile['tmp_name'], $filename, null, null, $conflictResolution);
     // Save changes to underlying record (if it's a DataObject)
     if ($this->file instanceof DataObject) {
         $this->file->write();
     }
     //to allow extensions to e.g. create a version after an upload
     $this->file->extend('onAfterUpload');
     $this->extend('onAfterLoad', $this->file);
     return true;
 }
コード例 #2
0
 /**
  * Save an file passed from a form post into this object.
  * File names are filtered through {@link FileNameFilter}, see class documentation
  * on how to influence this behaviour.
  *
  * @param array $tmpFile
  * @param AssetContainer $file
  * @return bool True if the file was successfully saved into this record
  */
 public function loadIntoFile($tmpFile, $file = null, $folderPath = false)
 {
     $this->file = $file;
     // Validate filename
     $filename = $this->getValidFilename($tmpFile, $folderPath);
     if (!$filename) {
         return false;
     }
     $filename = $this->resolveExistingFile($filename);
     // Save changes to underlying record (if it's a DataObject)
     $this->storeTempFile($tmpFile, $filename, $this->file);
     if ($this->file instanceof DataObject) {
         $this->file->write();
     }
     //to allow extensions to e.g. create a version after an upload
     $this->file->extend('onAfterUpload');
     $this->extend('onAfterLoadIntoFile', $this->file);
     return true;
 }