在Symfony 2.8中上传多张图片

时间:2016-06-24 10:36:27

标签: php symfony file-upload twig

我正在研究Symfony 2.8,我已经制作了一张表格来上传1张图片,而且工作正常。但我现在遇到的问题是,我想让这个表格允许我们多张图片。 这是我尝试过的,但还没有结果!

图片实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;

/**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
 */
class Image
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="path", type="string", length=255)
     */
    private $path;

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank(message="Please, upload the product name as a image file.")
     * @Assert\File(mimeTypes={ "image/jpg", "image/jpeg" ,"image/png"})
     */
    private $name;


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_time", type="datetime", nullable=false)
     */
    private $createTime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="change_time", type="datetime", nullable=true)
     */
    private $changeTime;

    /**
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", cascade={"persist"})
     * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
     */
    private $createUser;

    /**
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", cascade={"persist"})
     * @ORM\JoinColumn(referencedColumnName="id", nullable=true)
     */
    private $changeUser;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set path
     *
     * @param string $path
     *
     * @return Image
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }


    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return \DateTime
     */
    public function getCreateTime()
    {
        return $this->createTime;
    }

    /**
     * @param \DateTime $createTime
     */
    public function setCreateTime($createTime)
    {
        $this->createTime = $createTime;
    }

    /**
     * @return \DateTime
     */
    public function getChangeTime()
    {
        return $this->changeTime;
    }

    /**
     * @param \DateTime $changeTime
     */
    public function setChangeTime($changeTime)
    {
        $this->changeTime = $changeTime;
    }

    /**
     * @return mixed
     */
    public function getChangeUser()
    {
        return $this->changeUser;
    }

    /**
     * @param mixed $changeUser
     */
    public function setChangeUser($changeUser)
    {
        $this->changeUser = $changeUser;
    }

    /**
     * @return mixed
     */
    public function getCreateUser()
    {
        return $this->createUser;
    }

    /**
     * @param mixed $createUser
     */
    public function setCreateUser($createUser)
    {
        $this->createUser = $createUser;
    }



}

ImageType:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ImageType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', FileType::class, array("label" => "Files",
                "required" => FALSE,
                "attr" => array(
                "multiple" => "multiple")))
            ->add('path')
            ->add('Upload', SubmitType::class)
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Image'
        ));
    }
}

ImageController:

public function UploadAction(Request $request)
{

    $product = new Image();
    $form = $this->createForm(ImageType::class, $product);
    $form->handleRequest($request);


    if ($form->isSubmitted() && $form->isValid()) {

        /** @var UploadedFile $file */
        $file = $product->getName();

        $fileName = md5(uniqid()).'.'.$file->guessExtension();


        $file->move(
            '/var/www/html/master_c/web/images/',
            $fileName
        );

        $product->setName($fileName);
        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

    }

    return $this->render('AppBundle:Image:upload.html.twig', array(
        'form' => $form->createView(),
    ));
}

upload.html.twig:

{{ form_start(form) }}
    {# ... #}

    {{ form_row(form.name,  { 'attr': {'multiple': 'multiple' }}) }}
{{ form_end(form) }}

2 个答案:

答案 0 :(得分:0)

创建多次上传需要在表单类型中创建一个集合类型,并使JavaScript在前端添加多个上传。您可以查看更多详细信息。 http://symfony.com/doc/current/reference/forms/types/collection.html

答案 1 :(得分:0)

您需要将此'multiple' => true 更改为

->add('name', FileType::class, array("label" => "Files",
                'required' => false,
                'multiple' => true))

你应该有这样的东西

cat /dev/hidraw0