如何解决无法发送多个文件系统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 举报,一经查实,本站将立刻删除。