i'm beginning symfony , i'm stuck multiple upload.
i can upload 1 file , try modify code multiple upload.
here's error :
expected argument of type "symfony\component\httpfoundation\file\uploadedfile", "array" given
here's code :
imagearticletype:
$builder ->add('file', 'file', array('label' => 'choisir mes images', 'multiple'=> true)) ;
and entity imagearticle.php
<?php namespace ad\platformbundle\entity; use doctrine\orm\mapping orm; use symfony\component\httpfoundation\file\uploadedfile; use symfony\component\validator\constraints 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; } } /** * 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; } /** * 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; } /** * 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 */ }
i thinking simple foreach loop doesn't work...
uploading files multiple
option can little bit tricky. multiple = false
option $file
property in entity class return single uploadedfile instance, multiple = true
returns array of uploadedfile instances when call $entity->getfile()
. that's why have implement upload process manually, on form submission, in action, without lifecycle callbacks. this:
action:
.... $image = new imagearticle(); $form = $this->createform('your_form', $image); $form->handlerequest($request); if ($form->isvalid()) { ... foreach ($image->getfile() $uploadedfile) { $image = new imagearticle(); $image // setters go here ; // upload process go here $image->setfile(null); } $this->get('doctrine.orm.entity_manager')->flush(); .... }
Comments
Post a Comment