problème au cours d'uplaod plusieurs fichiers en symfony2

Par med001, il y a 10 ans


Salut , j'ai un problème au cours d'upload plusieurs fichiers en symfony 2

ca le problème

code d'entite Article

<?php namespace CrudBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; use CrudBundle\Entity\File; /** * Article * * @ORM\Table() * @ORM\Entity(repositoryClass="CrudBundle\Entity\ArticleRepository") */ class Article { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="nomarticle", type="string", length=255) */ private $nomarticle; /** * @var string * * @ORM\Column(name="contenuarticle", type="string", length=255) */ private $contenuarticle; /** * @ORM\Column(type="string", length=255, nullable=true) */ public $path; /** * @Assert\File(maxSize="6000000") */ // private $files; /** * @var File * * @ORM\OneToMany(targetEntity="File", mappedBy="Article", cascade={"persist"}) * */ private $files; /** * @var ArrayCollection */ private $uploadedFiles; /** * @ORM\OneToMany(targetEntity="Matche", mappedBy="articles") */ private $matche; /** * @ORM\OneToMany(targetEntity="Equipe", mappedBy="article") */ private $equipe; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nomarticle * * @param string $nomarticle * @return Article */ public function setNomarticle($nomarticle) { $this->nomarticle = $nomarticle; return $this; } /** * Get nomarticle * * @return string */ public function getNomarticle() { return $this->nomarticle; } /** * Set contenuarticle * * @param string $contenuarticle * @return Article */ public function setContenuarticle($contenuarticle) { $this->contenuarticle = $contenuarticle; return $this; } /** * Get contenuarticle * * @return string */ public function getContenuarticle() { return $this->contenuarticle; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__.'/../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'uploads/article'; } /** * Sets files. * * @param UploadedFile $files */ public function setFiles(array $files){ foreach ($files as $file){ $this->files = $files; } } /** * Get files. * * @return UploadedFile */ public function getFiles() { return $this->files; } /*public function upload() { // the file property can be empty if the field is not required if (null === $this->getFile()) { return; } // use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the // target filename to move to $this->getFile()->move( $this->getUploadRootDir(), $this->getFile()->getClientOriginalName() ); // set the path property to the filename where you've saved the file $this->path = $this->getFile()->getClientOriginalName(); // clean up the file property as you won't need it anymore $this->file = null; } */ /** * @ORM\PreFlush() */ public function upload() { foreach($this->files as $file) { $path = sha1(uniqid(mt_rand(), true)).'.'.$file->guessExtension(); array_push ($this->paths, $path); $file->move($this->getUploadRootDir(), $path); unset($file); } } /** * Set path * * @param string $path * @return Article */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Constructor */ public function __construct() { $this->matche = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add matche * * @param \CrudBundle\Entity\Matche $matche * @return Article */ public function addMatche(\CrudBundle\Entity\Matche $matche) { $this->matche[] = $matche; return $this; } /** * Remove matche * * @param \CrudBundle\Entity\Matche $matche */ public function removeMatche(\CrudBundle\Entity\Matche $matche) { $this->matche->removeElement($matche); } /** * Get matche * * @return \Doctrine\Common\Collections\Collection */ public function getMatche() { return $this->matche; } /** * Add equipe * * @param \CrudBundle\Entity\Equipe $equipe * @return Article */ public function addEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe[] = $equipe; return $this; } /** * Remove equipe * * @param \CrudBundle\Entity\Equipe $equipe */ public function removeEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe->removeElement($equipe); } /** * Get equipe * * @return \Doctrine\Common\Collections\Collection */ public function getEquipe() { return $this->equipe; } }

code d'articleType:

<?php namespace CrudBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use CrudBundle\Form\FilesType; class ArticleType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options / public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('nomarticle') ->add('contenuarticle', 'textarea', array('attr' => array('class' => 'ckeditor'))) ->add('files', new FilesType(), array( 'label' => 'Photos', 'required' => true, 'attr' => array( 'accept' => 'article/', ) )) ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'CrudBundle\Entity\Article' )); } /** * @return string */ public function getName() { return 'crudbundle_article'; } } ?>

Entity File :

<?php namespace CrudBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="files") * @ORM\Entity */ class File { /** * @var integer * * @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; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @var integer * * @ORM\Column(name="size", type="integer") */ private $size; /** * @var UploadedFile */ private $file; /** * @ORM\ManyToOne(targetEntity="Article", inversedBy="files") * @ORM\JoinColumn(name="article_id", referencedColumnName="id") **/ private $article; }

8 réponses

skp, il y a 10 ans

Salut, "$this->paths" n'existe pas dans ton entité.

med001, il y a 10 ans

je modifier l'entité article , je remplace path par paths mais toujours la même probléme

<?php namespace CrudBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; use CrudBundle\Entity\File; /** * Article * * @ORM\Table() * @ORM\Entity(repositoryClass="CrudBundle\Entity\ArticleRepository") */ class Article { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="nomarticle", type="string", length=255) */ private $nomarticle; /** * @var string * * @ORM\Column(name="contenuarticle", type="string", length=255) */ private $contenuarticle; /** * @ORM\Column(type="string", length=255, nullable=true) */ public $paths; /** * @Assert\File(maxSize="6000000") */ // private $files; /** * @var File * * @ORM\OneToMany(targetEntity="File", mappedBy="Article", cascade={"persist"}) * */ private $files; /** * @var ArrayCollection */ private $uploadedFiles; /** * @ORM\OneToMany(targetEntity="Matche", mappedBy="articles") */ private $matche; /** * @ORM\OneToMany(targetEntity="Equipe", mappedBy="article") */ private $equipe; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nomarticle * * @param string $nomarticle * @return Article */ public function setNomarticle($nomarticle) { $this->nomarticle = $nomarticle; return $this; } /** * Get nomarticle * * @return string */ public function getNomarticle() { return $this->nomarticle; } /** * Set contenuarticle * * @param string $contenuarticle * @return Article */ public function setContenuarticle($contenuarticle) { $this->contenuarticle = $contenuarticle; return $this; } /** * Get contenuarticle * * @return string */ public function getContenuarticle() { return $this->contenuarticle; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__.'/../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'uploads/article'; } /** * Sets files. * * @param UploadedFile $files */ public function setFiles(array $files){ foreach ($files as $file){ $this->files = $files; } } /** * Get files. * * @return UploadedFile */ public function getFiles() { return $this->files; } /*public function upload() { // the file property can be empty if the field is not required if (null === $this->getFile()) { return; } // use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the // target filename to move to $this->getFile()->move( $this->getUploadRootDir(), $this->getFile()->getClientOriginalName() ); // set the path property to the filename where you've saved the file $this->path = $this->getFile()->getClientOriginalName(); // clean up the file property as you won't need it anymore $this->file = null; } */ /** * @ORM\PreFlush() */ public function upload() { foreach($this->files as $file) { $path = sha1(uniqid(mt_rand(), true)).'.'.$file->guessExtension(); array_push ($this->paths, $path); $file->move($this->getUploadRootDir(), $path); unset($file); } } /** * Set path * * @param string $path * @return Article */ public function setPath($path) { $this->paths = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->paths; } /** * Constructor */ public function __construct() { $this->matche = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add matche * * @param \CrudBundle\Entity\Matche $matche * @return Article */ public function addMatche(\CrudBundle\Entity\Matche $matche) { $this->matche[] = $matche; return $this; } /** * Remove matche * * @param \CrudBundle\Entity\Matche $matche */ public function removeMatche(\CrudBundle\Entity\Matche $matche) { $this->matche->removeElement($matche); } /** * Get matche * * @return \Doctrine\Common\Collections\Collection */ public function getMatche() { return $this->matche; } /** * Add equipe * * @param \CrudBundle\Entity\Equipe $equipe * @return Article */ public function addEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe[] = $equipe; return $this; } /** * Remove equipe * * @param \CrudBundle\Entity\Equipe $equipe */ public function removeEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe->removeElement($equipe); } /** * Get equipe * * @return \Doctrine\Common\Collections\Collection */ public function getEquipe() { return $this->equipe; } }
skp, il y a 10 ans

"$this->paths" doit être de type array pour être utilisé avec array_push(). Tu as une erreur car, la première fois ton attribut "$this->paths" est égal à null.

med001, il y a 10 ans

je fais cà pour getPaths et setPaths mais la même erreur

/** * Set paths * * @param string $paths * @return Article */ public function setPaths(array $paths) { foreach ($paths as $path){ $this->paths = $paths; } } /** * Get path * * @return string */ public function getPaths() { return $this->paths; }
nico41, il y a 10 ans

Bonjour;

en plus des getter/setter tu doit initialiser $paths

private $paths = [];

sinon $paths sera égal à null, et donc erreur dans le array_push

med001, il y a 10 ans

je corrige un peux le code mais toujours il y'a des erreurs

code entity Article

<?php namespace CrudBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; use Doctrine\Common\Collections\ArrayCollection; use CrudBundle\Entity\File; /** * Article * * @ORM\Table() * @ORM\Entity(repositoryClass="CrudBundle\Entity\ArticleRepository") */ class Article { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="nomarticle", type="string", length=255) */ private $nomarticle; /** * @var string * * @ORM\Column(name="contenuarticle", type="string", length=255) */ private $contenuarticle; /** * @ORM\Column(type="array") */ private $paths; /** * @var File * * @ORM\OneToMany(targetEntity="File", mappedBy="Article", cascade={"persist"}) * */ private $files; /** * @var ArrayCollection */ private $uploadedFiles; /** * @ORM\OneToMany(targetEntity="Matche", mappedBy="articles") */ private $matche; /** * @ORM\OneToMany(targetEntity="Equipe", mappedBy="article") */ private $equipe; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nomarticle * * @param string $nomarticle * @return Article */ public function setNomarticle($nomarticle) { $this->nomarticle = $nomarticle; return $this; } /** * Get nomarticle * * @return string */ public function getNomarticle() { return $this->nomarticle; } /** * Set contenuarticle * * @param string $contenuarticle * @return Article */ public function setContenuarticle($contenuarticle) { $this->contenuarticle = $contenuarticle; return $this; } /** * Get contenuarticle * * @return string */ public function getContenuarticle() { return $this->contenuarticle; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__.'/../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'uploads/article'; } /** * Sets files. * * @param UploadedFile $files */ public function setFiles(array $files){ foreach ($files as $files){ $this->files = $files; } } /** * Get files. * * @return UploadedFile */ public function getFiles() { return $this->files; } /** * Set paths * * @param string $paths * @return Article */ public function setPath(array $paths) { $this->path = $paths; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Constructor */ public function __construct() { $this->path = new \Doctrine\Common\Collections\ArrayCollection(); $this->matche = new \Doctrine\Common\Collections\ArrayCollection(); } /** * @ORM\PreFlush() */ public function upload() { foreach($this->uploadedFiles as $uploadedFile) { $file = new File(); /* * These lines could be moved to the File Class constructor to factorize * the File initialization and thus allow other classes to own Files */ $path = sha1(uniqid(mt_rand(), true)).'.'.$uploadedFile->guessExtension(); $file->setPath($path); $file->setSize($uploadedFile->getClientSize()); $file->setName($uploadedFile->getClientOriginalName()); $uploadedFile->move($this->getUploadRootDir(), $path); $this->getFiles()->add($file); $file->setDocument($this); unset($uploadedFile); } } /** * Add matche * * @param \CrudBundle\Entity\Matche $matche * @return Article */ public function addMatche(\CrudBundle\Entity\Matche $matche) { $this->matche[] = $matche; return $this; } /** * Remove matche * * @param \CrudBundle\Entity\Matche $matche */ public function removeMatche(\CrudBundle\Entity\Matche $matche) { $this->matche->removeElement($matche); } /** * Get matche * * @return \Doctrine\Common\Collections\Collection */ public function getMatche() { return $this->matche; } /** * Add equipe * * @param \CrudBundle\Entity\Equipe $equipe * @return Article */ public function addEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe[] = $equipe; return $this; } /** * Remove equipe * * @param \CrudBundle\Entity\Equipe $equipe */ public function removeEquipe(\CrudBundle\Entity\Equipe $equipe) { $this->equipe->removeElement($equipe); } /** * Get equipe * * @return \Doctrine\Common\Collections\Collection */ public function getEquipe() { return $this->equipe; } }

code Entity ArticleController

<?php namespace CrudBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use CrudBundle\Entity\Article; use CrudBundle\Form\ArticleType; use Symfony\Component\HttpFoundation\Response; class ArticleController extends Controller { public function ajouterAction() { $msg= "Ajouter Article"; $em = $this->getDoctrine()->getManager(); $arti = new Article(); $form = $this->createForm(new ArticleType,$arti); $request = $this->getRequest(); if($request->getMethod()=='POST') { $form->handleRequest($request); if ($form->isValid()) { $arti->upload(); $em->persist($arti); $em->flush(); $msg="Article ajoutée avec success :)"; } } return $this->render('CrudBundle:Article:ajouter.html.twig',array( 'form'=>$form->createView(), 'msg'=>$msg ) ); } public function afficherAction() { $em = $this->getDoctrine()->getManager(); $actu = $em->getRepository('CrudBundle:Article')->findAll(); return $this->render('CrudBundle:Article:afficher.html.twig',array('actus'=>$actu)); } public function modifierAction($id) { $msg= "Modifier Article"; $em = $this->getDoctrine()->getManager(); $arti = $em->getRepository('CrudBundle:Article')->find($id); $form = $this->createForm(new ArticleType,$arti); $request = $this->getRequest(); if($request->getMethod()=='POST') { $form->handleRequest($request); if ($form->isValid()) { $arti->upload(); $em->flush(); $msg="Article Modifiée avec success :)"; } } return $this->render('CrudBundle:Article:modifier.html.twig',array( 'form'=>$form->createView(), 'msg'=>$msg ) ); } public function supprimerAction($id) { $em=$this->getDoctrine()->getManager(); $arti = $em->find('CrudBundle:Article',$id); if(!$arti) { throw $this->createNotFoundException('Article avec l\' id '.$id.' n\'existe pas'); } $em->remove($arti); $em->flush(); return new Response("Article supprimée avec succes"); } }
Lartak, il y a 10 ans

Bonjour.
Pour commencer, tu as un problème à la fonction setFiles :

foreach ($files as $files)

Les deux variables dans le foreach se nomment exactement pareil, ça ne te choque pas ?

med001, il y a 10 ans

bonjour Lartak,

/** * @ORM\Column(type="array") */ private $paths; /** * @var File * * @ORM\OneToMany(targetEntity="File", mappedBy="Article", cascade={"persist"}) * */ private $files; ** * Sets file. * * @param UploadedFile $files */ public function setFiles(array $files){ foreach ($files as $file){ $this->files = $file; } } /** * Get file. * * @return UploadedFile */ public function getFiles() { return $this->files; } /** * Set path * * @param string $paths * @return Article */ public function setPath(array $paths) { foreach($paths as $path) { $this->paths = $path; } } /** * Get path * * @return string */ public function getPath() { return $this->path; }

mais je trouve ce probléme

http://www5.0zz0.com/2016/03/31/13/795611686.png