无法发送多个文件系统Synfony 4

问题描述

我正在尝试从Symfony 4表单发送多个文件,但仅上传了最后一个文件。 我创建了一个功能'envoiFichier()',但我认为其中出现了问题,因为第一个文件名发送到数据库,但只有最后一个文件上载。

这是控制器代码:

    ...
    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;
    }

...

谢谢您的帮助 附言我不想使用集合来管理文件。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...