Created
February 25, 2019 08:40
-
-
Save kurozumi/32eaa8e99a8a65d9443a83ff1830bf1e to your computer and use it in GitHub Desktop.
FileUploader Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Service; | |
use Symfony\Component\HttpFoundation\File\Exception\FileException; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
/** | |
* Description of FileUploader | |
* | |
*/ | |
class FileUploader { | |
private $targetDirectory; | |
public function __construct($targetDirectory) { | |
$this->targetDirectory = $targetDirectory; | |
} | |
public function upload(UploadedFile $file) { | |
$fileName = md5(uniqid()) . '.' . $file->guessExtension(); | |
try { | |
$file->move($this->getTargetDirectory(), $fileName); | |
} catch (Exception $e) { | |
} | |
return $fileName; | |
} | |
public function getTargetDirectory() { | |
return $this->targetDirectory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment