phpsymfonysymfony4vichuploaderbundle

VichUploader symfony 4 not uploading image


I am trying to upload an image into the path : {project}/public/img/circuits ,using vichuploaderBundle.

I followed his github documentation : https://github.com/dustin10/VichUploaderBundle/blob/master/docs/index.md

The situation is : I have a form with a VichImageType and when I submit said form with an image I get this :

thumbnailFile: Symfony\Component\HttpFoundation\File\UploadedFile {#91 ▼
    -test: false
    -originalName: "meme.jpg"
    -mimeType: "image/jpeg"
    -error: 0
    path: "/tmp"
    filename: "phpomKjXK"
    basename: "phpomKjXK"
    pathname: "/tmp/phpomKjXK"
    extension: ""
    realPath: "/tmp/phpomKjXK"
    aTime: 2020-11-26 16:22:10
    mTime: 2020-11-26 16:22:10
    cTime: 2020-11-26 16:22:10
    inode: 262126
    size: 79694
    perms: 0100600
    owner: 1000
    group: 1000
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }

I use this mapping (vich_uploader.yaml):

vich_uploader:
    db_driver: orm

    mappings:
        circuits:
            uri_prefix: /img/circuits
            upload_destination: '%kernel.project_dir%/public/img/circuits'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer


Here are the pieces of code related to the image from my entity :

/**
 * @ORM\Entity(repositoryClass=CircuitRepository::class)
 * @Vich\Uploadable
 */
class Circuit
{
/**
     * @ORM\Column(type="string", length=255)
     */
    private $thumbnail;

    /**
     * @Vich\UploadableField(mapping="circuits", fileNameProperty="thumbnail")
     * @var File|null
     */
    private $thumbnailFile;
    /**
     * @var \DateTime
     */
    private $updated_at;

    public function getThumbnail(): ?string
    {
        return $this->thumbnail;
    }

    public function setThumbnail(string $thumbnail): self
    {
        $this->thumbnail = $thumbnail;

        return $this;
    }


    /**
     * @return File
     */
    public function getThumbnailFile(): ?File
    {
        return $this->thumbnailFile;
    }

    /**
     * @param File $thumbnailFile
     */
    public function setThumbnailFile(?File $thumbnailFile): self
    {
        $this->thumbnailFile = $thumbnailFile;
        if ($this->thumbnailFile instanceof UploadedFile)
            $this->updated_at = new \DateTime('now');
        return $this;
    }

}

Finally here is the part of my form that handles the image input :

->add('thumbnailFile', VichImageType::class, [
                'label' => 'Vignette',
                'allow_delete' => false,
                'download_uri' => false,
                'image_uri' => false,
                'required' => true,
            ])

Does anyone know why the files I try to upload aren't saved in my /public/img/circuits folder ?

Thanks.

EDIT : When I create my entity the image uploads and saves fine but onces I try modifying it and override the existing image of the entity with another one it erase it from the entity but it doesn't upload the new one.


Solution

  • I found a workaround.

    So, it seems weird but for some reasons it works. I needed to when handling the form submission set the value of thumbnail (the image name variable) manually with it's own getter. And now I can override an image with another one.

    $circuit->setThumbnail($circuit->getThumbnail());