| Current Path : /var/www/v3.cesa.co.za/src/CoreBundle/Services/ |
| Current File : /var/www/v3.cesa.co.za/src/CoreBundle/Services/FileUploader.php |
<?php
namespace App\CoreBundle\Services;
use Symfony\Component\String\Slugger\SluggerInterface;
/**
* Description of FileUploader
*
* @author otto
*/
class FileUploader {
public function __construct(
private SluggerInterface $slugger,
) {
}
public function upload(UploadedFile $file, string $targetDirectory): string {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $this->slugger->slug($originalFilename);
$fileName = $safeFilename . '-' . uniqid() . '.' . $file->guessExtension();
try {
$file->move($targetDirectory, $fileName);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
return $fileName;
}
}