Newer
Older
<?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;
}
}