Esempio n. 1
0
 /**
  * Upload resource
  *
  * @param array $data    File data
  * @param array $params  Path parameters
  * @param bool  $useTemp Use temporary state
  *
  * @throws \InvalidArgumentException
  * @throws \ErrorException
  * @return Resource|null
  */
 public function upload($data, $params = array(), $useTemp = true)
 {
     if (empty($data)) {
         throw new \InvalidArgumentException("File data to be uploaded is empty.");
     }
     $resource = null;
     switch ($data['error']) {
         case UPLOAD_ERR_OK:
             $source = new File($data['name']);
             $temp = new File($data['tmp_name']);
             $params = array_merge(array('filename' => $source->getFileName(), 'extension' => $source->getExtension()), $params);
             $resource = $this->map(static::UPLOAD, $params);
             $target = $resource->getFile();
             $temp->move($target);
             break;
         case UPLOAD_ERR_NO_FILE:
             if ($useTemp) {
                 $resource = $this->map(static::UPLOAD, $params);
             } else {
                 throw new \ErrorException("File has not been uploaded.");
             }
             break;
         case UPLOAD_ERR_INI_SIZE:
             throw new \ErrorException(sprintf("Uploaded file size exceeds server limit: %d MB", Php::get('upload_max_filesize')));
             break;
         case UPLOAD_ERR_FORM_SIZE:
             throw new \ErrorException("Uploaded file size exceeds form limit.");
             break;
         case UPLOAD_ERR_PARTIAL:
             throw new \ErrorException("Uploaded file is only partially completed.");
             break;
         case UPLOAD_ERR_NO_TMP_DIR:
             throw new \ErrorException("Missing temporary directory for uploaded file.");
             break;
         case UPLOAD_ERR_CANT_WRITE:
             throw new \ErrorException("Failed to write uploaded file to disk.");
             break;
         case UPLOAD_ERR_EXTENSION:
         default:
             throw new \ErrorException("Unknown upload error.");
             break;
     }
     return $resource;
 }