#Symfony upload only images

i try to learn Symfony with exercice and i stuck with upload images.
In my exercice, They need to add only images. But apparently they can add others files than images.
I don’t see what they want because for me, it’s work.

 class UploadFileType extends AbstractType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
{
     $builder->add('file', FileType::class, [
         'attr' => [
             'accept' => "image/*"
         ]
     ]);
 }
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Upload::class
    ]);
}
class Upload
{
   protected ?SplFileInfo $file;

   public function getFile(): ?SplFileInfo
   {
       return $this->file;
   }
class UploadController extends AbstractController
{
    /**
     * @Route("/upload", name="upload")
     */
    public function upload(Request $request)
    {
        $upload = new Upload();
 
        $form = $this->createForm(UploadFileType::class, $upload);
 
        $form->handleRequest($request);
 
        if ($form->isSubmitted() && $form->isValid()) {
            if ($upload->getFile()) {
                // sauvegarde du fichier, pas a implementer
            }
        }
 
        return $this->render('Exercices/upload.html.twig', [
            'form' => $form->createView()
        ]);
    }
}
 
    public function setFile(?SplFileInfo $file): void
    {
        $this->file = $file;
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *