/**
  * Creates Symfony UploadedFile instance from PSR-7 ones.
  *
  * @param UploadedFileInterface $psrUploadedFile
  *
  * @return UploadedFile
  */
 private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
 {
     $temporaryPath = '';
     $clientFileName = '';
     if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
         $temporaryPath = $this->getTemporaryPath();
         $psrUploadedFile->moveTo($temporaryPath);
         $clientFileName = $psrUploadedFile->getClientFilename();
     }
     return new UploadedFile($temporaryPath, null === $clientFileName ? '' : $clientFileName, $psrUploadedFile->getClientMediaType(), $psrUploadedFile->getSize(), $psrUploadedFile->getError(), true);
 }
예제 #2
0
파일: Uploader.php 프로젝트: wandu/http
 /**
  * @param \Psr\Http\Message\UploadedFileInterface $file
  * @return string
  */
 public function uploadFile(UploadedFileInterface $file)
 {
     if ($file->getError() === UploadedFile::OK) {
         $fileName = pathinfo($file->getClientFilename());
         $dir = date("ymd");
         if (!file_exists("{$this->basePath}/{$dir}")) {
             if (false === @mkdir("{$this->basePath}/{$dir}", 0777, true)) {
                 throw new RuntimeException("fail to create directory.");
             }
         }
         do {
             $newFileName = sha1($fileName['filename'] . rand());
             $newFilePath = "{$this->basePath}/{$dir}/{$newFileName}.{$fileName['extension']}";
         } while (file_exists($newFilePath));
         $file->moveTo($newFilePath);
         return "{$dir}/{$newFileName}.{$fileName['extension']}";
     }
 }
예제 #3
0
 /**
  * Checks if an error during upload occured
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
  * @throws \Aimeos\Controller\Common\Exception If an error occured during upload
  */
 protected function checkFileUpload(\Psr\Http\Message\UploadedFileInterface $file)
 {
     if ($file->getError() !== UPLOAD_ERR_OK) {
         switch ($file->getError()) {
             case UPLOAD_ERR_INI_SIZE:
             case UPLOAD_ERR_FORM_SIZE:
                 throw new \Aimeos\Controller\Common\Exception('The uploaded file exceeds the max. allowed filesize');
             case UPLOAD_ERR_PARTIAL:
                 throw new \Aimeos\Controller\Common\Exception('The uploaded file was only partially uploaded');
             case UPLOAD_ERR_NO_FILE:
                 throw new \Aimeos\Controller\Common\Exception('No file was uploaded');
             case UPLOAD_ERR_NO_TMP_DIR:
                 throw new \Aimeos\Controller\Common\Exception('Temporary folder is missing');
             case UPLOAD_ERR_CANT_WRITE:
                 throw new \Aimeos\Controller\Common\Exception('Failed to write file to disk');
             case UPLOAD_ERR_EXTENSION:
                 throw new \Aimeos\Controller\Common\Exception('File upload stopped by extension');
             default:
                 throw new \Aimeos\Controller\Common\Exception('Unknown upload error');
         }
     }
 }
 /**
  * Convert a single file back into an array.
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file The file to convert.
  * @return array
  */
 protected static function convertFile($file)
 {
     return ['name' => $file->getClientFilename(), 'type' => $file->getClientMediaType(), 'tmp_name' => $file->getStream()->getMetadata('uri'), 'error' => $file->getError(), 'size' => $file->getSize()];
 }
예제 #5
0
 /**
  * Convert a single file back into an array.
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file The file to convert.
  * @return array
  */
 protected static function convertFile($file)
 {
     $error = $file->getError();
     $tmpName = '';
     if ($error === UPLOAD_ERR_OK) {
         $tmpName = $file->getStream()->getMetadata('uri');
     }
     return ['name' => $file->getClientFilename(), 'type' => $file->getClientMediaType(), 'tmp_name' => $tmpName, 'error' => $error, 'size' => $file->getSize()];
 }