phpsymfonyfile-uploadvichuploaderbundleflysystem

Vich Uploader integrate with Fly System cannot upload file but saved in database


I tried to create file upload by using vich uploader integrate with fly system in symfony.

However I only can save the file name in database but not uploading.

Is it something wrong with the flysystem or vich uploader setting?

Vich Uploader setting

vich_uploader:
    db_driver: orm
    storage: flysystem

    mappings:
        uploadedfile:
            uri_prefix: /uploads
            upload_destination: uploads.storage
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

flysystem setting

flysystem:
  storages:
    uploads.storage:
      adapter: 'local'
      options:
        directory: '%kernel.project_dir%/public/uploads'

Entity

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\ImportFileRepository;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[ORM\Entity(repositoryClass: ImportFileRepository::class)]
#[Vich\Uploadable]
class ImportFile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'string', length: 255)]
    private $importFileName;

    #[Vich\UploadableField(mapping: 'uploadedfile', fileNameProperty: 'importFileName')]
    private ?File $importFile;

    #[ORM\Column(type: 'datetime', columnDefinition: 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP')]
    private $createdDate;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $importFile
     */
    public function setImportFile(?File $importFile = null): self
    {
        $this->importFile = $importFile;

        if (null !== $importFile) {
            $this->createdDate = new \DateTime('now');
        }

        return $this;
    }

    public function getImportFile(): ?File
    {
        return $this->importFile;
    }

    public function setImportFileName($importFileName): self
    {
        $this->importFileName = $importFileName;

        return $this;
    }

    public function getImportFileName(): ?string
    {
        return $this->importFileName;
    }

    public function getCreatedDate(): ?\DateTimeInterface
    {
        return $this->createdDate;
    }

    public function setCreatedDate(\DateTimeInterface $createdDate): self
    {
        $this->createdDate = $createdDate;

        return $this;
    }

Controller

/**
     *For build the form to twig
     * @return Response
     */
    #[Route('/import', name: 'import_index')]
    public function index(): Response
    {
        // Build form for import csv
        $form = $this->createFormBuilder()
            ->add('upload_file', VichFileType::class, [
                'required' => true,
                'allow_delete' => true,
                'asset_helper' => true,
                'attr' => ['class' => 'form-control'],
            ])
            ->getForm();

        return $this->render('/admin/import/index.html.twig', ['form' => $form->createView(),]);
    }


/**
     * For persist data and file
     * Save file by Vich Uploader integrate with FlySystem
     * @param $file
     * @return void
     */
    private function saveFile($file){
        $uploadFile = new ImportFile();

        $uploadFile->setImportFileName($file->getClientOriginalName());
        $uploadFile->setImportFile($file);

        $this->entityManager->persist($uploadFile);
        $this->entityManager->flush();

    }

Solution

  • Because you are using php attributes, it looks like you may need to set the metadata.type key in your vich_uploader config (docs)

    vich_uploader:
        metadata:
            type: attribute