Skip to content
Snippets Groups Projects
FileUploader.php 823 B
Newer Older
Адлан Шамавов's avatar
fix
Адлан Шамавов committed
<?php

namespace App\Service;

use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileUploader
{
    private $targetDirectory;

    public function __construct($targetDirectory)
    {
        $this->targetDirectory = $targetDirectory;
    }

    public function upload(UploadedFile $file)
    {
        $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) .'.'. $file->guessExtension();
        try {
            $file->move($this->getTargetDirectory(), $fileName);
        } catch (FileException $e) {
            throw new FileException($e->getMessage());
        }

        return $this->getTargetDirectory() . $fileName;
    }

    public function getTargetDirectory()
    {
        return $this->targetDirectory;
    }
}