Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created February 25, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/32eaa8e99a8a65d9443a83ff1830bf1e to your computer and use it in GitHub Desktop.
Save kurozumi/32eaa8e99a8a65d9443a83ff1830bf1e to your computer and use it in GitHub Desktop.
FileUploader Service
<?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