gpt4 book ai didi

php - 多次上传 Symfony2

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:44 24 4
gpt4 key购买 nike

我刚开始使用 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 循环,但它不起作用...

最佳答案

使用 multiple 选项上传文件可能有点棘手。使用 multiple = false 选项,您实体类中的 $file 属性将返回单个 UploadedFile 实例,使用 multiple = true 它将返回 UploadedFile 实例数组当您调用 $entity->getFile() 时。这就是为什么您必须在没有生命周期回调的情况下,在提交表单时在您的操作中手动实现上传过程。像这样:

行动:

....
$image = new ImageArticle();

$form = $this->createForm('YOUR_FORM', $image);
$form->handleRequest($request);

if ($form->isValid()) {
...

foreach ($image->getFile() as $uploadedFile) {
$image = new ImageArticle();
$image
// setters go here
;

// Upload process go here

$image->setFile(null);
}

$this->get('doctrine.orm.entity_manager')->flush();

....
}

这是我项目的截图: enter image description here

关于php - 多次上传 Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38833050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com