How to upload .jpg file in my form Symfony 5

816 Views Asked by At

Impossible for me to upload .jpg files in Symfony 5.1, i tried many things, If i search on the mimtype guide in symfony documentation (https://www.iana.org/assignments/media-types/media-types.xhtml), there is no simple.jpg supported, so how can I do this Its okay for other files formats. last try: (I would like to upload image and pdf only)

use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\Image;

class CapebType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $builder
        ->add('name', FileType::class, [
            'label'=> 'Chargez ma carte carte Capeb',
            'required'   => true,
            'constraints' => [
                new File([
                    'maxSize' => '5M',
                    'mimeTypes' => [
                        'image/jpeg,', 'image/pjpeg', 'image/png', 'image/gif', 'image/jpg',
                        'application/pdf',
                        'application/x-pdf',
                    ],
                    'mimeTypesMessage' => 'Le fichier n\'est pas valide, assurez vous d\'avoir un fichier au format PDF, PNG, JPG, JPEG)',
                ]),
            ]
        ])
        ->add('submit', SubmitType::class, [
            'label'=> 'Envoyez ma carte CAPEB',
        ])
    ;
    }
1

There are 1 best solutions below

0
On

Simple, need juste to add image/x

$builder
            ->add('name', FileType::class, [
                'label'=> 'Chargez ma carte carte Capeb',
                'required'   => true,
                'constraints' => [
                    new File([
                        'maxSize' => '5M',
                        'mimeTypes' => [
                            'image/*',
                            'application/pdf',
                            'application/x-pdf',
                        ],
                        'mimeTypesMessage' => 'Le fichier n\'est pas valide, assurez vous d\'avoir un fichier au format PDF, PNG, JPG, JPEG)',
                    ]),
                ]
            ])
            ->add('submit', SubmitType::class, [
                'label'=> 'Envoyez ma carte CAPEB',
            ])
        ;