Xpressengine\Storage\Storage::upload PHP Méthode

upload() public méthode

file upload to storage
public upload ( Symfony\Component\HttpFoundation\File\UploadedFile $uploaded, string $path, string | null $name = null, string | null $disk = null, Xpressengine\User\UserInterface $user = null ) : File
$uploaded Symfony\Component\HttpFoundation\File\UploadedFile uploaded file instance
$path string be saved path
$name string | null be saved file name
$disk string | null disk name (ex. local, ftp, s3 ...)
$user Xpressengine\User\UserInterface user instance
Résultat File
    public function upload(UploadedFile $uploaded, $path, $name = null, $disk = null, UserInterface $user = null)
    {
        if ($uploaded->isValid() === false) {
            throw new InvalidFileException(['name' => $uploaded->getClientOriginalName(), 'detail' => $uploaded->getErrorMessage()]);
        }
        $id = $this->keygen->generate();
        $name = $name ?: $this->makeFilename($uploaded->getClientOriginalName());
        $path = $this->makePath($id, $path);
        $disk = $disk ?: $this->distributor->allot($uploaded);
        $user = $user ?: $this->auth->user();
        if (!$this->files->store(file_get_contents($uploaded->getPathname()), $path . '/' . $name, $disk)) {
            throw new WritingFailException();
        }
        $file = $this->createModel();
        $file->id = $id;
        $file->userId = $user->getId();
        $file->disk = $disk;
        $file->path = $path;
        $file->filename = $name;
        $file->clientname = $uploaded->getClientOriginalName();
        $file->mime = $uploaded->getMimeType();
        $file->size = $uploaded->getSize();
        $file->save();
        return $file;
    }

Usage Example

 public function testUploadThrownExceptionWhenFileIsInvalid()
 {
     list($handler, $repo, $auth, $keygen) = $this->getMocks();
     $uploaded = m::mock('Symfony\\Component\\HttpFoundation\\File\\UploadedFile');
     $uploaded->shouldReceive('isValid')->andReturn(false);
     $uploaded->shouldReceive('getClientOriginalName')->andReturn('foo.jpg');
     $instance = new Storage($handler, $repo, $auth, $keygen);
     try {
         $instance->upload($uploaded, 'attached');
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertInstanceOf('Xpressengine\\Storage\\Exceptions\\InvalidFileException', $e);
     }
 }
All Usage Examples Of Xpressengine\Storage\Storage::upload