Cannot send multiple Filesystem Synfony 4

I’m trying to send multiple files from my Symfony 4 form but only the last file is uploaded.
I created a fonction ‘envoiFichier()’ but I think I made something wrong in it because the first file names are sent to the database but only the last file is uploaded.

Here is the controller code :

    ...
    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
        $em = $this->getDoctrine()->getManager();
        
        // Envoi des fichiers
        $photoEquipement = $form['photoEquipement']->getData();
        if ($photoEquipement) {
            $newFilenamePhotoEquipement = $this->envoiFichier($photoEquipement, $photoEquipementBdd);
            $equipementIntervention->setPhotoEquipement($newFilenamePhotoEquipement);
            unset($newFilenamePhotoEquipement);
        } else {
            $equipementIntervention->setPhotoEquipement($photoEquipementBdd);
        }
        
        $photoGraffiti = $form['photoGraffiti']->getData();
        if ($photoGraffiti) {
            $newFilenamePhotoGraffiti = $this->envoiFichier($photoGraffiti, $photoGrafittiBdd);
            $equipementIntervention->setPhotoGraffiti($newFilenamePhotoGraffiti);
            unset($newFilenamePhotoGraffiti);
        } else {
            $equipementIntervention->setPhotoGraffiti($photoGrafittiBdd);
        }
        
        $photoGraffitiApres = $form['photoGraffitiApres']->getData();
        if ($photoGraffitiApres) {
            $newFilenamePhotoGraffitiApres = $this->envoiFichier($photoGraffitiApres, $photoGrafittiApresBdd);
            $equipementIntervention->setPhotoGraffitiApres($newFilenamePhotoGraffitiApres);
            unset($newFilenamePhotoGraffitiApres);
        } else {
            $equipementIntervention->setPhotoGraffitiApres($photoGrafittiApresBdd);
        }
        
        $em->persist($equipementIntervention);
...
    public function envoiFichier($fileToTransfer, $fichier)
    {
        // init file system
        $filesystem = new Filesystem();
        $current_dir_path = getcwd();
        
        // suppression de la photo globale déjà uploadée si besoin
        $fileToDelete = $this->getParameter('photoEquipementIntervention_dir') . '/' . $fichier;
        if ($filesystem->exists($fileToDelete)){
            dump('Suppression');
            try {
                $filesystem->remove($fileToDelete);
            } catch (IOExceptionInterface $exception) {
                echo "An error occurred while deleting your file at ".$exception->getPath();
            }
        }
    
        $originalFilename = pathinfo($fileToTransfer->getClientOriginalName(), PATHINFO_FILENAME);
        // this is needed to safely include the file name as part of the URL
        $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
        $newFilename = $safeFilename.'-'.uniqid().'.'.$fileToTransfer->guessExtension();

        // Move the file to the directory where photo global are stored
        try {
            $fileToTransfer->move(
                $this->getParameter('photoEquipementIntervention_dir'),
                $newFilename
            );
        } catch (FileException $e) {
            $this->addFlash('warning', 'Le fichier Photo globale n'a pas pu être uploadé.');
        }

        unset($fileToTransfer);
        unset($fichier);
        unset($fileToDelete);
        
        return $newFilename;
    }

...

Thank you for your help
P.S. I don’t want to manage files with collection.

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

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