我正在使用Symfony 2.8,我做了一個表單上傳1張圖片,並且工作正常。但我現在的問題是,我想讓這種形式讓我們有多張照片。 這是我試過的,這是沒有結果!在Symfony 2.8上傳多張圖片
圖片實體:
<?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) }}
多文件上傳: http://stackoverflow.com/questions/6736946/multiple-file-upload-with-symfony2我希望這可以幫助 –
已經看到它,但它不幫助我不幸! – Shosiria