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);
        } else {
        $photoGraffiti = $form['photoGraffiti']->getData();
        if ($photoGraffiti) {
            $newFilenamePhotoGraffiti = $this->envoiFichier($photoGraffiti, $photoGrafittiBdd);
        } else {
        $photoGraffitiApres = $form['photoGraffitiApres']->getData();
        if ($photoGraffitiApres) {
            $newFilenamePhotoGraffitiApres = $this->envoiFichier($photoGraffitiApres, $photoGrafittiApresBdd);
        } else {
    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)){
            try {
            } 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 {
        } catch (FileException $e) {
            $this->addFlash('warning', 'Le fichier Photo globale n'a pas pu être uploadé.');

        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 *