/
vladislavts
/
gcppmsp
Обзор
Документация
Войти
/
vladislavts
/
gcppmsp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
prod
src/Services/FileUploader.php
37 строк
1 KB
Vladislav
Работает создание записей,
02 июл 2022, 16:09
02 июл 2022, 16:09
7bd28d9
Код
Авторство
О чём код?
<?php namespace App\Services; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\String\Slugger\SluggerInterface; class FileUploader { private SluggerInterface $slugger; private string $uploadsPath; public function __construct(string $uploadsPath ,SluggerInterface $slugger) { $this->slugger = $slugger; $this->uploadsPath = $uploadsPath; } public function uploadFile(File $file, ?string $oldFileName = null) { $fileName = $this->slugger ->slug(pathinfo($file instanceof UploadedFile ? $file->getClientOriginalName() : $file->getFilename(), PATHINFO_FILENAME)) ->append('-', uniqid()) ->append('.'. $file->guessExtension()) ->toString() ; $newFile = $file->move($this->uploadsPath, $fileName); if ($oldFileName && file_exists($this->uploadsPath . '/' . $oldFileName)) unlink($this->uploadsPath . '/' . $oldFileName); return $fileName; } }