public function prepare() { $this->file = $this->entity->getUploadedFile(); if ($this->file === null) { return; } $this->entity->setPath($this->entity->getFileBaseName() . '.' . $this->file->guessExtension()); }
* @ORM\Column(type="string",length=255, nullable=true) */ public $path; /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ public $file; public $tempFile; public $oldFile; public function getUploadRootDir() { return __DIR__ . '/../../../web/uploads/images';
public function upload() { // the file property can be empty if the field is not required if (null === $this->file) { return; } // set the path property to the filename where you will save the file $this->setUrl(uniqid() . '.' . $this->file->guessExtension()); // we use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the target filename to move to $this->file->move($this->getUploadRootDir(), $this->getUrl()); // clean up the file property as you won't need it anymore $this->file = null; }