>更新在我的Symfony项目中,我可以上传单个图像。现在我正在尝试上传多张图片。
class ImageFile extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('images', FileType::class, array(
'attr' => array(
'accept' => 'image/*',
'multiple' => 'multiple'
)
))
->add('save',SubmitType::class,array('label'=>'Insert Image','attr'=>array('class'=>'btn btn-primary','style'=>'margin-bottom:15px')))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => images::class
));
}
}
在我的图像实体中,我保留了一个像这样的 SatellieImage 对象数组。
class images
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @var satelliteImage[]
* @ORMColumn(name="images", type="string")
*
*/
private $images;
/**
* Set images
*
* @param string $images
*
* @return satelliteImage[]
*/
public function setImages($images)
{
$this->images = $images;
return $this->images;
}
/**
* Get images
*
* @return satelliteImage[]
*/
public function getImages()
{
return $this->images;
}
public function addImage($image)
{
$this->images[] = $image;
return $this;
}
}
在我的控制器中,我将 satelliteImage[] 存储为 $files 并运行一个 foreach 循环来上传() 每个文件(upload() 函数非常适合单个文件)
public function uploadImages(Request $request)
{
$satelliteImage=new satelliteImage;
$images=new images();
$form = $this->createForm(ImageFile::class, $images);
$form->handleRequest($request);
$files=$images->getImages();
foreach ($files as $file){
$em=$this->getDoctrine()->getManager();
$file->upload();
$em->persist($satelliteImage);
$em->flush();
}
$this->addFlash(
'notice',
'Images inserted successfully'
);
return $this->redirectToRoute('satellite_images');
}
问题:我收到一条错误消息,说"警告:为 foreach() 提供的参数无效"如何解决此问题?
我认为您在这里要做的就是将"multiple"属性添加到您的ImageFile
类中;
请参阅Symfony文档。
use SymfonyComponentFormExtensionCoreTypeFileType;
class ImageFile extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('images', FileType::class, [
'multiple' => true,
'attr' => [
'accept' => 'image/*',
'multiple' => 'multiple'
]
])
;
}
}
此外,将"图像"属性更改为"图像"属性:
/**
* Set images
*
* @param string $images
*
* @return satelliteImage[]
*/
public function setImages($images)
{
$this->images = $images;
return $this;
}
/**
* Get images
*
* @return string
*/
public function getImages()
{
return $this->image;
}
public function addImage($image)
{
$this->images[] = $image;
return $this;
}
完成排序后,您可以修改 upload
方法以处理 FormType 中的多个文件。
也许工作示例对您有所帮助?我为symfony 3.*制作了带有多重文件上传的简单应用程序。这是链接: https://github.com/marekz/example_symfony_multiply_files_example
第一:两种形式声明:
<?php
namespace AppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use AppBundleFormFilesType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
class UserType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('lastName')
->add('files', CollectionType::class,array(
'entry_type' => FilesType::class,
'allow_add' => true,
'by_reference' => false,
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundleEntityUser'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_user';
}
}
<?php
namespace AppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
class FilesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file');
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundleEntityFiles'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_files';
}
}
现在,我的实体:
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* User
*
* @ORMTable(name="user")
* @ORMEntity(repositoryClass="AppBundleRepositoryUserRepository")
*/
class User {
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORMColumn(name="lastName", type="string", length=255)
*/
private $lastName;
/**
* @ORMManyToMany(targetEntity="Files", cascade={"persist"})
*/
private $files;
function __construct() {
$this->files = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Set lastName
*
* @param string $lastName
*
* @return User
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* Get files
*
* @return ArrayCollection
*/
function getFiles() {
return $this->files;
}
/**
* Set files
* @param type $files
*/
function setFiles($files) {
$this->files = $files;
}
}
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
/**
* Files
*
* @ORMTable(name="files")
* @ORMEntity(repositoryClass="AppBundleRepositoryFilesRepository")
*/
class Files
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="file", type="string", length=255, unique=true)
* @AssertNotBlank(message="Please, upload the product brochure as a PDF file.")
* @AssertFile(mimeTypes={ "application/pdf" })
*/
private $file;
/**
*
* @return Files
*/
function getUser() {
return $this->user;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set file
*
* @param string $file
*
* @return Files
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* @return string
*/
public function getFile()
{
return $this->file;
}
}
最后,Symfony Controller:
/**
* Creates a new user entity.
*
* @Route("/new", name="user_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request) {
$user = new User();
$form = $this->createForm('AppBundleFormUserType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$attachments = $user->getFiles();
if ($attachments) {
foreach($attachments as $attachment)
{
$file = $attachment->getFile();
var_dump($attachment);
$filename = md5(uniqid()) . '.' .$file->guessExtension();
$file->move(
$this->getParameter('upload_path'), $filename
);
var_dump($filename);
$attachment->setFile($filename);
}
}
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_show', array('id' => $user->getId()));
}
return $this->render('user/new.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
<</div>
div class="one_answers">我发现这篇文章解释了symfony 5中这个问题的每一个细节。我对了解正在发生的事情非常有帮助。如果您是新手,可以从这里开始,然后阅读问题,您将同步。
文章: https://nouvelle-techno.fr/actualites/live-coding-upload-dimages-multiples-avec-symfony-4-et-5
功劳归于 Nouvelle-Techno.fr
文章是法语的,如果您使用 chrome 翻译成英文,请单击 url 栏上的translate this page
。