コード例 #1
0
 /**
  * Creates Symfony UploadedFile instance from PSR-7 ones.
  *
  * @param UploadedFileInterface $psrUploadedFile
  *
  * @return UploadedFile
  */
 private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
 {
     $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
ファイル: Client.php プロジェクト: sunkan/php-mogilefs
 /**
  * Upload data to tracker
  *
  * @param string|StreamInterface|UploadedFileInterface $file
  * @param string $key
  * @param string $class
  * @param bool $use_file
  *
  * @return bool
  * @throws Exception
  */
 public function put($file, $key, $class, $use_file = true)
 {
     if ($key === null) {
         throw new InvalidArgumentException(get_class($this) . "::put key cannot be null");
     }
     if ($use_file) {
         if ($file instanceof UploadedFileInterface) {
             $fh = $file->getStream()->detach();
             $length = $file->getSize();
         } elseif ($file instanceof StreamInterface) {
             $fh = $file->detach();
             $length = $file->getSize();
         } elseif (is_resource($file) && get_resource_type($file) == 'stream') {
             $fh = $file;
         } else {
             $fh = fopen($file, 'r');
         }
         if (!$fh) {
             throw new RuntimeException(get_class($this) . "::put failed to open file");
         }
         if (!$length) {
             $length = filesize($file);
         }
     } else {
         $fh = fopen('php://memory', 'rw');
         if ($fh === false) {
             throw new RuntimeException(get_class($this) . "::put failed to open memory stream");
         }
         fwrite($fh, $file);
         rewind($fh);
         $length = strlen($file);
     }
     //CREATE_OPEN domain=%s&key=%s&class=%s&multi_dest=%d
     $location = $this->doRequest('CREATE_OPEN', ['domain' => $this->domain, 'key' => $key, 'class' => $class]);
     $uri = $location['path'];
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_INFILE, $fh);
     curl_setopt($ch, CURLOPT_INFILESIZE, $length);
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->request_timeout);
     curl_setopt($ch, CURLOPT_PUT, $this->request_timeout);
     curl_setopt($ch, CURLOPT_URL, $uri);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect: ']);
     $response = curl_exec($ch);
     fclose($fh);
     if ($response === false) {
         $error = curl_error($ch);
         curl_close($ch);
         throw new RuntimeException(get_class($this) . "::put {$error}");
     }
     curl_close($ch);
     $this->doRequest('CREATE_CLOSE', ['key' => $key, 'class' => $class, 'domain' => $this->domain, 'devid' => $location['devid'], 'fid' => $location['fid'], 'path' => urldecode($uri)]);
     return true;
 }
コード例 #3
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)
 {
     return ['name' => $file->getClientFilename(), 'type' => $file->getClientMediaType(), 'tmp_name' => $file->getStream()->getMetadata('uri'), 'error' => $file->getError(), 'size' => $file->getSize()];
 }
コード例 #4
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()];
 }