Symfony 6 -如何将文件上传改为多个文件上传

  • 本文关键字:文件 Symfony symfony validation
  • 更新时间 :
  • 英文 :


我正在做一个用户可以上传文件的项目。我的代码在上传单个文件时工作,但我需要更改它,以便用户能够上传多个文件。

我想把文件存储在我的数据库作为字符串。目前它被存储为示例:"file1.png"。当上传多个文件时,我希望它被存储为"file1.png;file2.png;file3.png"。然而,当我加上"multiple =>true"在表单中,当验证器按下submit时,我得到一个错误,输入需要是String。

我最好的猜测是,我需要使用数据转换器,但阅读文档后,我仍然不知道如何接近这个。

?

数据变换这是控制器(目前它期望一个文件,对于多个我将使用foreach):

#[Route('/new', name: 'app_blog_new', methods: ['GET', 'POST'])]
#[IsGranted('IS_AUTHENTICATED')]
public function new(Request $request, BlogRepository $blogRepository, SluggerInterface $slugger, MailerInterface $mailer): Response
{
$blog = new Blog();
$form = $this->createForm(BlogType::class, $blog);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$additionalImages = $form->get('additional_images')->getData();
if ($additionalImages) {
$originalFilename = pathinfo($additionalImages->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename . '-' . uniqid() . '.' . $additionalImages->guessExtension();

try {
$additionalImages->move(
$this->getParameter('blogimage_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}

$blog->setAdditionalImages($newFilename);
}
}

如果我加上"multiple =>对于这种形式,我得到一个'预期字符串'前面有错误。这是用来上传图片到博客的表单:


public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
->add('additional_images', FileType::class, [
'label' => 'Additional images',
'mapped' => false,
'multiple' => true,
'required' => false,
'constraints' => [`your text`
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'image/*',
],
'mimeTypesMessage' => 'Please upload a valid image',
])
],
]);
$builder->get('additional_images')
->addModelTransformer(new CallbackTransformer(
function ($additionalAsArray) {
// transform the array to a string
return implode('; ', $additionalAsArray);
},
function ($additionalAsString) {
// transform the string back to an array
return explode('; ', $additionalAsString);
}
))
;
}

这是包含图片的博客实体类

#[ORMEntity(repositoryClass: BlogRepository::class)]
class Blog
{
#[ORMColumn(type: Types::TEXT, nullable: true)]
private ?string $additional_images = null;
}

我试着添加'multiple =>为True,它可以工作,因为用户能够选择多个文件。但提交后,我得到"implode():参数#1 ($pieces)必须是类型数组,字符串给定">

我发现我所要做的就是添加"new all "到表单:

->add('additional_images', FileType::class, [
'label' => 'Additional images',
'mapped' => false,
'required' => false,
'multiple' => true,
'constraints' => [
new All([
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'image/*',
],
'mimeTypesMessage' => 'Please upload a valid image',
])
])
],
]);

并使我的控制器与数组一起工作:

$additionalImages = $form->get('additional_images')->getData();
if ($additionalImages) {
$result = array();
foreach ($additionalImages as $image)
{
$originalFilename = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename . '-' . uniqid() . '.' . $image->guessExtension();
try {
$image->move(
$this->getParameter('blogimage_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$result[] = $newFilename;
}
$blog->setAdditionalImages(implode(";", $result));

}

相关内容

  • 没有找到相关文章

最新更新