以PHP格式上载多个文件
我目前一次上传一个图像文件到mysql数据库,但是我需要上传800个图像,所以我想选择所有图像并通过点击上传它们。这是我的PHP控制器以PHP格式上载多个文件,php,symfony,Php,Symfony,我目前一次上传一个图像文件到mysql数据库,但是我需要上传800个图像,所以我想选择所有图像并通过点击上传它们。这是我的PHP控制器 /** * @Route("/insert", name="satellite_images_create") */ public function insertAction(Request $request) { $satelliteImage=new satelliteImage; $fo
/**
* @Route("/insert", name="satellite_images_create")
*/
public function insertAction(Request $request)
{
$satelliteImage=new satelliteImage;
$form=$this->createFormBuilder($satelliteImage)
->add('file')
->add('save',SubmitType::class,array('label'=>'Insert Image','attr'=>array('class'=>'btn btn-primary','style'=>'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em=$this->getDoctrine()->getManager();
$satelliteImage->upload();
$em->persist($satelliteImage);
$em->flush();
$this->addFlash(
'notice',
'Image inserted successfully'
);
return $this->redirectToRoute('satellite_images');
}
return $this->render('satelliteImages/insert.html.twig',array(
'form'=>$form->createView()));
}
卫星图像实体具有处理上传的功能
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
$imgFile=$this->getFile();
$this->setImage(file_get_contents($imgFile));
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
这是我的树枝档案
{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header">Insert Image</h2>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
{%extends'base.html.twig%}
{%block body%}
插入图像
{{form_start(form)}}
{{form_widget(form)}
{{form_end(form)}}
{%endblock%}
如何修改表单和上载功能,以便选择要上载的所有图像文件?谢谢。我建议您签出DropzoneJS 它是一个javascript前端,用于处理多个文件上传。它通过将文件传递到PHP后端进行存储/处理来工作 编辑-添加 如果您需要有关如何将DropzoneJS与symfony2一起使用的信息,请查看
此外,这个问题基本上是重复的你的模板应该生成必要的HTML,以允许多个文件上传,也就是说,可以这样做。但是我如何在控制器中处理它?前端很好,我的问题是如何在PHP后端处理它们基本上你需要调用你的
upload()
每个文件的函数。前端表单应单独发送每个文件。DropzoneJS就是这样工作的。如果你试图在一篇大文章中发送所有800个图像,你可能会遇到上传最大文件大小
和上传最大文件大小
的问题。我还想说,如果你的PHP后端目前只处理一个文件,那么后端是好的,问题是前端。你只需要让前端分别将每个文件发送到PHP后端。是的,我可以上传单个图像。但问题是,我如何才能将多个文件发送到后端。如果可能的话,我可以简单地为每个文件调用upload()函数。据我所知,表单是为一个实体创建的。这是否正确?可能有一种方法可以像jeff建议的那样使用。但是,您必须在控制器中将图像作为数组进行处理,我不确定您将如何进行处理。您可以查看PHP文档@中的lookphp at gmail.com示例以了解更多信息。