【发布时间】:2016-12-14 10:56:03
【问题描述】:
我是从 Symfony 开始的,但我遇到了多次上传问题。
我只能上传一个文件,我尝试修改此代码以进行多次上传。
这是我的错误:
Expected argument of type "Symfony\Component\HttpFoundation\File\UploadedFile", "array" given
这是我的代码:
ImageArticleType:
$builder
->add('file', 'file', array('label' => 'Choisir mes images', 'multiple'=> true))
;
还有我的实体ImageArticle.php
<?php
namespace AD\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* ImageArticle
*
* @ORM\Table()
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
*/
class ImageArticle
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* @var File
*
* @Assert\File(
* maxSize = "1M",
* mimeTypes = {
* "image/jpeg",
* "image/gif",
* "image/png",
* },
* maxSizeMessage = "La taille maximum du fichier doit etre inférieur ou égale à 1MB. Pour reduire sa taille vous pouvez utiliser le site : compressjpeg.com",
* mimeTypesMessage = "Seulement les fichiers .jpeg / .gif /.png sont acceptés"
* )
*/
private $file;
private $tempFileName;
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (null !== $this->url)
{
$this->tempFileName = $this->url;
$this->url=null;
$this->alt=null;
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set url
*
* @param string $url
*
* @return ImageArticle
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set alt
*
* @param string $alt
*
* @return ImageArticle
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null === $this->file)
{
return;
}
//On add un extension pour le fichier.
$this->url = $this->file->guessExtension();
//Le alt est le nom du fichier du client.
$this->alt= $this->file->getClientOriginalName();
}
/**
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*
*/
public function upload()
{
if(null=== $this->file)
{
return;
}
//Si ancien fichier on supprime
if(null !== $this->tempFileName)
{
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFileName;
if (file_exists($oldFile))
{
unlink($oldFile);
}
}
//On deplace
$this->file->move
(
$this->getUploadRootDir(),
$this->id.'.'.$this->url
);
}
/**
*@ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFileName = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}
/**
*
* @ORM\PostRemove()
*/
public function removeUpload()
{
if(file_exists($this->tempFileName))
{
unlink($this->tempFileName);
}
}
public function getUploadDir()
{
return 'upload/img/';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function __toString()
{
return $this->getUploadDir().$this->id.'.'.$this->getUrl();
}
/**
* Set source
*
* @param string $source
*
* @return Image
*/
}
我在考虑一个简单的 foreach 循环,但它不起作用......
【问题讨论】:
-
您的错误是告诉您您尝试传递
array而不是UploadedFile类的项目。尝试从您的字段中删除multiple选项,这可能会呈现一个数组。 -
感谢您的回复!如果我删除多个选项,我不能选择多个文件。
-
是的,我知道,但是如果您仍然收到只有一个文件的错误,请尝试。如果不是你的代码是正确的,我认为你应该尝试在你的控制器中创建一个
foreach循环,单独保存每个文件。 -
哦,好的。是的,只有一个文件可以正常工作。在我的控制器中,我坚持/刷新我的文章。我尝试在我的文章中包含不止一张图片,因此在 ArticleType 中我将 ImageArticleType 称为
->add('imagearticle', new ImageArticleType(), array('required' => true))。我不能在我的 ImageArticle 实体中创建一个 foreach 吗? -
不,在你的控制器里面,看看下面的答案