Can’t upload file VichUploaderBundle Symfony

  doctrine-orm, php, symfony, vichuploaderbundle

I am using Symfony and am trying to use Vich’s uploader bundle to upload a picture as an attribute of my class Property. I am aware that there is a known bug when no other field is updated, so to correct this I added a attribute updated_at that changes every time a picture is added (cf function setImageFile() in my Property.php class). However, even with that the upload displays a "success" popup, and yet the image file doesn’t appear in the intended directory nor does its name appear in the database. Here are the relevant parts of my files:

  • my config vich_uploader.yaml

    vich_uploader:
        db_driver: orm
    
        mappings:
            property_image:
                uri_prefix: /images/properties
                upload_destination: '%kernel.project_dir%/public/images/properties'
                namer: VichUploaderBundleNamingSmartUniqueNamer

  • my class Poperty.php:

    <?php
    
    namespace AppEntity;
    
    use DateTime;
    use CocurSlugifySlugify;
    use DoctrineORMMapping as ORM;
    use AppRepositoryPropertyRepository;
    use DoctrineCommonCollectionsCollection;
    use SymfonyComponentHttpFoundationFileFile;
    use DoctrineCommonCollectionsArrayCollection;
    use VichUploaderBundleMappingAnnotation as Vich;
    use SymfonyComponentValidatorConstraints as Assert;
    use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
    use SymfonyComponentHttpFoundationFileUploadedFile;
    
    /**
     * @ORMEntity(repositoryClass=PropertyRepository::class)
     * @UniqueEntity("title")
     * VichUploadable()
     */
    class Property
    {
       /**
         * @var string|null
         * @ORMColumn(type="string", length=255)
         */
        private $filename;
    
        /**
         * @var File|null
         * @VichUploadableField(mapping="property_image", fileNameProperty="filename")
         */
        private $imageFile;
    
        /**
         * @ORMColumn(type="datetime")
         */
        private $updated_at;
    
        /**
         * Set the value of imageFile
         *
         * @param  File|null  $imageFile
         *
         * @return  self
         */ 
        public function setImageFile(?File $imageFile):Property
        {
            $this->imageFile = $imageFile;
            if ($this->imageFile instanceof UploadedFile) {
                $this->updated_at = new DateTime('now');
            }
            return $this;
        }

  • my form class PropertyType.php

    <?php
    
    namespace AppForm;
    
    use AppEntityOption;
    use AppEntityProperty;
    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormBuilderInterface;
    use SymfonyBridgeDoctrineFormTypeEntityType;
    use SymfonyComponentOptionsResolverOptionsResolver;
    use SymfonyComponentFormExtensionCoreTypeChoiceType;
    use SymfonyComponentFormExtensionCoreTypeFileType;
    
    class PropertyType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('imageFile', FileType::class, [
                    'required' => false
                ])
            ;
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'data_class' => Property::class,
            ]);
        }
    }

Source: Symfony Questions

LEAVE A COMMENT