resq/src/Service/FileUploader.php

34 lines
751 B
PHP

<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $target_dir;
public function __construct($target_dir)
{
$this->target_dir = $target_dir;
}
public function upload(UploadedFile $file)
{
do
{
//$filename = md5(uniqid()) . '.' . $file->guessExtension();
$filename = md5(uniqid()) . '.' . $file->getClientOriginalExtension();
}
while(file_exists($this->getTargetDir() . '/' . $filename));
$file->move($this->getTargetDir(), $filename);
return $filename;
}
public function getTargetDir()
{
return $this->target_dir;
}
}