2017-03-09 83 views
0

Symfony3與PhpStorm.2016.3.2多文件上傳 - symfony3

我成功地使一個文件上傳的只有一張圖片。但現在我需要使它成爲「多個」

我會告訴你代碼和它出現的錯誤,因爲我無法上傳多個文件。

這裏是控制器

public function newAction(Request $request) 
{ 
    $restaurant = new Restaurant(); 
    $form = $this->createForm(RestaurantType::class, $restaurant); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 

     // file upload 
     if ($request->files->get('restaurant')['picture'] != null) { 

      $file = $request->files->get('restaurant')['picture']; 
      $targetDir = $this->getParameter('uploaded_restaurants'); 
      $filename = $this->get('app.uploader')->upload($file, $targetDir); 
      $mediaRestaurant = null; 
      if ($restaurant->getId() != null) { 
       $mediaRestaurant = $this->getDoctrine()->getRepository('AppBundle:Media')->findPictureByRestaurant($restaurant); 
      } 
      if ($mediaRestaurant) { 
       $media = $mediaRestaurant; 
       $fs = new Filesystem(); 
       try { 
        $fs->remove($this->get('kernel')->getRootDir().'/../web/uploads/restaurants/'.$media->getName()); 
       } catch (IOException $e) { 
        echo "error"; 
       } 
      } else { 
       $media = new Media(); 
       $media->setCreatedAt(new \DateTime()); 
      } 

      $originalName = $file->getClientOriginalName(); 
      $media->setName($filename) 
       ->setOriginalName($originalName) 
       ->setType('img') 
       ->setContext('restaurant_picture') 
       ->setUpdatedAt(new \DateTime()) 
      ; 
      $media->setRestaurant($restaurant); 
      $em->persist($media); 
     } 

     $restaurant->setIsActivated(false); 
     $em->persist($restaurant); 
     $em->flush(); 
    } 

    return $this->render('admin/restaurant/new.html.twig', array(
     'restaurant' => $restaurant, 
     'form' => $form->createView(), 
    )); 
} 

我FormType(稱爲RestaurantType),我添加文件字段上傳(當我設置multiplefalse它的實際工作只有一個圖片)

$builder 
     ->add('picture', FileType::class, array(
      'label'     => 'Photos du restaurant', 
      'multiple'    => true, 
      'required'    => false, 
      'mapped'    => false, 
      'attr'     => array(
       'accept'    => '.jpg,.jpeg,.png'), 
     )) 

上載的服務文件

class FileUploader 
{ 
    /** 
    * @param UploadedFile $file 
    * @param $targetDir 
    * @return string 
    */ 
    public function upload(UploadedFile $file, $targetDir) 
    { 
     $fileName = md5(uniqid()).'.'.$file->guessExtension(); 
     $file->move($targetDir, $fileName); 
     return $fileName; 
    } 
} 

在MediaRepository

public function findPictureByRestaurant(Restaurant $restaurant) 
{ 
    return $this->createQueryBuilder('m') 
     ->select('m') 
     ->where('m.restaurant = :restaurant') 
     ->setParameter('restaurant', $restaurant) 
     ->andWhere('m.context = :restaurant_picture') 
     ->setParameter('restaurant_picture', 'restaurant_picture') 
     ->getQuery() 
     ->getOneOrNullResult(); 
} 

在我show.html.twig 的QueryBuilder的,你可以看到圖片

{% if restaurant.medias != null and restaurant.medias.count > 0 and restaurant.medias[0].name != null %} 
     <img src="/uploads/restaurants/{{ restaurant.medias[0].name }}"> 
{% endif %} 

我config.yml其中文件上傳

parameters: 
    locale: fr 
     uploaded_restaurants: "%kernel.root_dir%/../web/uploads/restaurants" 

與實體Restaurant這是相當長(對不起)

/** 
* Restaurant 
* 
* @ORM\Table(name="restaurant") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\RestaurantRepository") 
*/ 
class Restaurant 
{ 
    /** 
    * @var FoodType 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\FoodType", inversedBy="restaurants") 
    * @ORM\JoinColumn(name="food_type_id", referencedColumnName="id") 
    */ 
    private $foodType; 

    /** 
    * @var City 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\City", inversedBy="restaurants") 
    * @ORM\JoinColumn(name="city_id", referencedColumnName="id") 
    */ 
    private $city; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Media", mappedBy="restaurant") 
    */ 
    private $medias; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Privatisation", mappedBy="restaurant") 
    */ 
    private $privatisations; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Retrocession", mappedBy="restaurant") 
    */ 
    private $retrocessions; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\OpenedSlot", mappedBy="restaurant") 
    */ 
    private $openedSlots; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\ExceptionSlot", mappedBy="restaurant") 
    */ 
    private $exceptionSlots; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Slot", mappedBy="restaurant") 
    */ 
    private $slots; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Chef", mappedBy="restaurant") 
    */ 
    private $chefs; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->medias = new ArrayCollection(); 
     $this->privatisations = new ArrayCollection(); 
     $this->retrocessions = new ArrayCollection(); 
     $this->openedSlots = new ArrayCollection(); 
     $this->exceptionSlots = new ArrayCollection(); 
     $this->slots = new ArrayCollection(); 
     $this->chefs = new ArrayCollection(); 
    } 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=120, unique=true) 
    * @Assert\Length(
    *  max = 120, 
    *  maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="slug", type="string", length=255, unique=true) 
    * @Gedmo\Slug(fields={"name"}) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $slug; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="description", type="text") 
    */ 
    private $description; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="webUrl", type="string", length=255, nullable=true) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $webUrl; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="tripAdvisorUrl", type="string", length=255, nullable=true) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $tripAdvisorUrl; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="facebookUrl", type="string", length=255, nullable=true) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $facebookUrl; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="twitterUrl", type="string", length=255, nullable=true) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $twitterUrl; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="instagramUrl", type="string", length=255, nullable=true) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $instagramUrl; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="phone", type="string", length=20) 
    * @Assert\Regex(
    *  pattern="#^0[1-9]([-. ]?[0-9]{2}){4}$#", 
    *  match=true, 
    *  message="Numéro de téléphone invalide." 
    * ) 
    */ 
    private $phone; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="phone2", type="string", length=20, nullable=true) 
    * @Assert\Regex(
    *  pattern="#^0[1-9]([-. ]?[0-9]{2}){4}$#", 
    *  match=true, 
    *  message="Numéro de téléphone invalide." 
    * ) 
    */ 
    private $phone2; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="email", type="string", length=255) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $email; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="latitude", type="float") 
    * @Assert\Regex(
    *  pattern="/^-?(?:\d+|\d*\.\d+)$/", 
    *  match=true, 
    * ) 
    */ 
    private $latitude; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="longitude", type="float") 
    * @Assert\Regex(
    *  pattern="/^-?(?:\d+|\d*\.\d+)$/", 
    *  match=true, 
    * ) 
    */ 
    private $longitude; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="stars", type="integer", nullable=true) 
    * @Assert\Regex(
    *  pattern="/^[0-9]+$/", 
    *  match=true, 
    *  message="Ceci n'est pas un chiffre." 
    * ) 
    */ 
    private $stars; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="seatNumber", type="integer", nullable=true) 
    * @Assert\Regex(
    *  pattern="/^[0-9]+$/", 
    *  match=true, 
    * ) 
    */ 
    private $seatNumber; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="minPrice", type="float", nullable=true) 
    * @Assert\Regex(
    *  pattern="/^-?(?:\d+|\d*\.\d+)$/", 
    *  match=true, 
    * ) 
    */ 
    private $minPrice; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="maxPrice", type="float", nullable=true) 
    * @Assert\Regex(
    *  pattern="/^-?(?:\d+|\d*\.\d+)$/", 
    *  match=true, 
    * ) 
    */ 
    private $maxPrice; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="address", type="string", length=255) 
    * @Assert\Length(
    * max = 255, 
    * maxMessage = "Ce champ ne peut pas dépasser {{ limit }} caractères." 
    * ) 
    */ 
    private $address; 

    /** 
    * @var bool 
    * 
    * @ORM\Column(name="is_activated", type="boolean") 
    */ 
    private $isActivated = true; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Restaurant 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set slug 
    * 
    * @param string $slug 
    * 
    * @return Restaurant 
    */ 
    public function setSlug($slug) 
    { 
     $this->slug = $slug; 

     return $this; 
    } 

    /** 
    * Get slug 
    * 
    * @return string 
    */ 
    public function getSlug() 
    { 
     return $this->slug; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * 
    * @return Restaurant 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Set webUrl 
    * 
    * @param string $webUrl 
    * 
    * @return Restaurant 
    */ 
    public function setWebUrl($webUrl) 
    { 
     $this->webUrl = $webUrl; 

     return $this; 
    } 

    /** 
    * Get webUrl 
    * 
    * @return string 
    */ 
    public function getWebUrl() 
    { 
     return $this->webUrl; 
    } 

    /** 
    * Set tripAdvisorUrl 
    * 
    * @param string $tripAdvisorUrl 
    * 
    * @return Restaurant 
    */ 
    public function setTripAdvisorUrl($tripAdvisorUrl) 
    { 
     $this->tripAdvisorUrl = $tripAdvisorUrl; 

     return $this; 
    } 

    /** 
    * Get tripAdvisorUrl 
    * 
    * @return string 
    */ 
    public function getTripAdvisorUrl() 
    { 
     return $this->tripAdvisorUrl; 
    } 

    /** 
    * Set facebookUrl 
    * 
    * @param string $facebookUrl 
    * 
    * @return Restaurant 
    */ 
    public function setFacebookUrl($facebookUrl) 
    { 
     $this->facebookUrl = $facebookUrl; 

     return $this; 
    } 

    /** 
    * Get facebookUrl 
    * 
    * @return string 
    */ 
    public function getFacebookUrl() 
    { 
     return $this->facebookUrl; 
    } 

    /** 
    * Set twitterUrl 
    * 
    * @param string $twitterUrl 
    * 
    * @return Restaurant 
    */ 
    public function setTwitterUrl($twitterUrl) 
    { 
     $this->twitterUrl = $twitterUrl; 

     return $this; 
    } 

    /** 
    * Get twitterUrl 
    * 
    * @return string 
    */ 
    public function getTwitterUrl() 
    { 
     return $this->twitterUrl; 
    } 

    /** 
    * Set instagramUrl 
    * 
    * @param string $instagramUrl 
    * 
    * @return Restaurant 
    */ 
    public function setInstagramUrl($instagramUrl) 
    { 
     $this->instagramUrl = $instagramUrl; 

     return $this; 
    } 

    /** 
    * Get instagramUrl 
    * 
    * @return string 
    */ 
    public function getInstagramUrl() 
    { 
     return $this->instagramUrl; 
    } 

    /** 
    * Set phone 
    * 
    * @param string $phone 
    * 
    * @return Restaurant 
    */ 
    public function setPhone($phone) 
    { 
     $this->phone = $phone; 

     return $this; 
    } 

    /** 
    * Get phone 
    * 
    * @return string 
    */ 
    public function getPhone() 
    { 
     return $this->phone; 
    } 

    /** 
    * Set phone2 
    * 
    * @param string $phone2 
    * 
    * @return Restaurant 
    */ 
    public function setPhone2($phone2) 
    { 
     $this->phone2 = $phone2; 

     return $this; 
    } 

    /** 
    * Get phone2 
    * 
    * @return string 
    */ 
    public function getPhone2() 
    { 
     return $this->phone2; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * 
    * @return Restaurant 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Get email 
    * 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set latitude 
    * 
    * @param float $latitude 
    * 
    * @return Restaurant 
    */ 
    public function setLatitude($latitude) 
    { 
     $this->latitude = $latitude; 

     return $this; 
    } 

    /** 
    * Get latitude 
    * 
    * @return float 
    */ 
    public function getLatitude() 
    { 
     return $this->latitude; 
    } 

    /** 
    * Set longitude 
    * 
    * @param float $longitude 
    * 
    * @return Restaurant 
    */ 
    public function setLongitude($longitude) 
    { 
     $this->longitude = $longitude; 

     return $this; 
    } 

    /** 
    * Get longitude 
    * 
    * @return float 
    */ 
    public function getLongitude() 
    { 
     return $this->longitude; 
    } 

    /** 
    * Set stars 
    * 
    * @param integer $stars 
    * 
    * @return Restaurant 
    */ 
    public function setStars($stars) 
    { 
     $this->stars = $stars; 

     return $this; 
    } 

    /** 
    * Get stars 
    * 
    * @return int 
    */ 
    public function getStars() 
    { 
     return $this->stars; 
    } 

    /** 
    * Set seatNumber 
    * 
    * @param integer $seatNumber 
    * 
    * @return Restaurant 
    */ 
    public function setSeatNumber($seatNumber) 
    { 
     $this->seatNumber = $seatNumber; 

     return $this; 
    } 

    /** 
    * Get seatNumber 
    * 
    * @return int 
    */ 
    public function getSeatNumber() 
    { 
     return $this->seatNumber; 
    } 

    /** 
    * Set minPrice 
    * 
    * @param float $minPrice 
    * 
    * @return Restaurant 
    */ 
    public function setMinPrice($minPrice) 
    { 
     $this->minPrice = $minPrice; 

     return $this; 
    } 

    /** 
    * Get minPrice 
    * 
    * @return float 
    */ 
    public function getMinPrice() 
    { 
     return $this->minPrice; 
    } 

    /** 
    * Set maxPrice 
    * 
    * @param float $maxPrice 
    * 
    * @return Restaurant 
    */ 
    public function setMaxPrice($maxPrice) 
    { 
     $this->maxPrice = $maxPrice; 

     return $this; 
    } 

    /** 
    * Get maxPrice 
    * 
    * @return float 
    */ 
    public function getMaxPrice() 
    { 
     return $this->maxPrice; 
    } 

    /** 
    * Set address 
    * 
    * @param string $address 
    * 
    * @return Restaurant 
    */ 
    public function setAddress($address) 
    { 
     $this->address = $address; 

     return $this; 
    } 

    /** 
    * Get address 
    * 
    * @return string 
    */ 
    public function getAddress() 
    { 
     return $this->address; 
    } 

    /** 
    * Add media 
    * 
    * @param Media $media 
    * 
    * @return Restaurant 
    */ 
    public function addMedia(Media $media) 
    { 
     $this->medias[] = $media; 

     return $this; 
    } 

    /** 
    * Remove media 
    * 
    * @param Media $media 
    */ 
    public function removeMedia(Media $media) 
    { 
     $this->medias->removeElement($media); 
    } 

    /** 
    * Get medias 
    * 
    * @return ArrayCollection 
    */ 
    public function getMedias() 
    { 
     return $this->medias; 
    } 

    /** 
    * Set foodType 
    * 
    * @param FoodType $foodType 
    * 
    * @return Restaurant 
    */ 
    public function setFoodType(FoodType $foodType = null) 
    { 
     $this->foodType = $foodType; 

     return $this; 
    } 

    /** 
    * Get foodType 
    * 
    * @return FoodType 
    */ 
    public function getFoodType() 
    { 
     return $this->foodType; 
    } 

    /** 
    * Add privatisation 
    * 
    * @param Privatisation $privatisation 
    * 
    * @return Restaurant 
    */ 
    public function addPrivatisation(Privatisation $privatisation) 
    { 
     $this->privatisations[] = $privatisation; 

     return $this; 
    } 

    /** 
    * Remove privatisation 
    * 
    * @param Privatisation $privatisation 
    */ 
    public function removePrivatisation(Privatisation $privatisation) 
    { 
     $this->privatisations->removeElement($privatisation); 
    } 

    /** 
    * Get privatisations 
    * 
    * @return ArrayCollection 
    */ 
    public function getPrivatisations() 
    { 
     return $this->privatisations; 
    } 

    /** 
    * Add retrocession 
    * 
    * @param Retrocession $retrocession 
    * 
    * @return Restaurant 
    */ 
    public function addRetrocession(Retrocession $retrocession) 
    { 
     $this->retrocessions[] = $retrocession; 

     return $this; 
    } 

    /** 
    * Remove retrocession 
    * 
    * @param Retrocession $retrocession 
    */ 
    public function removeRetrocession(Retrocession $retrocession) 
    { 
     $this->retrocessions->removeElement($retrocession); 
    } 

    /** 
    * Get retrocessions 
    * 
    * @return ArrayCollection 
    */ 
    public function getRetrocessions() 
    { 
     return $this->retrocessions; 
    } 

    /** 
    * Add openedSlot 
    * 
    * @param OpenedSlot $openedSlot 
    * 
    * @return Restaurant 
    */ 
    public function addOpenedSlot(OpenedSlot $openedSlot) 
    { 
     $this->openedSlots[] = $openedSlot; 

     return $this; 
    } 

    /** 
    * Remove openedSlot 
    * 
    * @param OpenedSlot $openedSlot 
    */ 
    public function removeOpenedSlot(OpenedSlot $openedSlot) 
    { 
     $this->openedSlots->removeElement($openedSlot); 
    } 

    /** 
    * Get openedSlots 
    * 
    * @return ArrayCollection 
    */ 
    public function getOpenedSlots() 
    { 
     return $this->openedSlots; 
    } 

    /** 
    * Add exceptionSlot 
    * 
    * @param ExceptionSlot $exceptionSlot 
    * 
    * @return Restaurant 
    */ 
    public function addExceptionSlot(ExceptionSlot $exceptionSlot) 
    { 
     $this->exceptionSlots[] = $exceptionSlot; 

     return $this; 
    } 

    /** 
    * Remove exceptionSlot 
    * 
    * @param ExceptionSlot $exceptionSlot 
    */ 
    public function removeExceptionSlot(ExceptionSlot $exceptionSlot) 
    { 
     $this->exceptionSlots->removeElement($exceptionSlot); 
    } 

    /** 
    * Get exceptionSlots 
    * 
    * @return ArrayCollection 
    */ 
    public function getExceptionSlots() 
    { 
     return $this->exceptionSlots; 
    } 

    /** 
    * Add slot 
    * 
    * @param Slot $slot 
    * 
    * @return Restaurant 
    */ 
    public function addSlot(Slot $slot) 
    { 
     $this->slots[] = $slot; 

     return $this; 
    } 

    /** 
    * Remove slot 
    * 
    * @param Slot $slot 
    */ 
    public function removeSlot(Slot $slot) 
    { 
     $this->slots->removeElement($slot); 
    } 

    /** 
    * Get slots 
    * 
    * @return ArrayCollection 
    */ 
    public function getSlots() 
    { 
     return $this->slots; 
    } 

    /** 
    * Add chef 
    * 
    * @param Chef $chef 
    * 
    * @return Restaurant 
    */ 
    public function addChef(Chef $chef) 
    { 
     $this->chefs[] = $chef; 

     return $this; 
    } 

    /** 
    * Remove chef 
    * 
    * @param Chef $chef 
    */ 
    public function removeChef(Chef $chef) 
    { 
     $this->chefs->removeElement($chef); 
    } 

    /** 
    * Get chefs 
    * 
    * @return ArrayCollection 
    */ 
    public function getChefs() 
    { 
     return $this->chefs; 
    } 

    /** 
    * Set city 
    * 
    * @param City $city 
    * 
    * @return Restaurant 
    */ 
    public function setCity(City $city = null) 
    { 
     $this->city = $city; 

     return $this; 
    } 

    /** 
    * Get city 
    * 
    * @return City 
    */ 
    public function getCity() 
    { 
     return $this->city; 
    } 

    /** 
    * @return bool 
    */ 
    public function getIsActivated() 
    { 
     return $this->isActivated; 
    } 

    /** 
    * @param bool $isActivated 
    */ 
    public function setIsActivated($isActivated) 
    { 
     $this->isActivated = $isActivated; 
    } 
} 

,當我上傳的圖片,並點擊submit按鈕

enter image description here

在出現的錯誤綜上所述,當我設置multiple至時,此代碼僅適用於一張圖片在我的FormType。但後來我被困在多個文件上傳,我無法找到解決辦法。有人知道如何用我給你的代碼來處理它嗎?

謝謝

+0

給讀爲[文章](http://dancostinel.com/blog/web/blog/how-to-create-multiple-upload-functionality-using-symfony3) –

+0

謝謝@DanCostinel!非常讚賞這個! :) –

回答

1

因爲你的文件定義接受多個上傳,你需要修改控制器的上裝部分:

// file upload 
if ($request->files->get('restaurant')['picture'] != null) { 
    $files = $request->files->get('restaurant')['picture']; 
    foreach ($files as $file) { 
     $targetDir = $this->getParameter('uploaded_restaurants'); 
     $filename = $this->get('app.uploader')->upload($file, $targetDir); 
     $mediaRestaurant = null; 
     if ($restaurant->getId() != null) { 
      $mediaRestaurant = $this->getDoctrine()->getRepository('AppBundle:Media')->findPictureByRestaurant($restaurant); 
     } 
     if ($mediaRestaurant) { 
      $media = $mediaRestaurant; 
      $fs = new Filesystem(); 
      try { 
       $fs->remove($this->get('kernel')->getRootDir().'/../web/uploads/restaurants/'.$media->getName()); 
      } catch (IOException $e) { 
       echo "error"; 
      } 
     } else { 
      $media = new Media(); 
      $media->setCreatedAt(new \DateTime()); 
     } 
     $originalName = $file->getClientOriginalName(); 
     $media->setName($filename) 
      ->setOriginalName($originalName) 
      ->setType('img') 
      ->setContext('restaurant_picture') 
      ->setUpdatedAt(new \DateTime()) 
      ; 
     $media->setRestaurant($restaurant); 
     $em->persist($media); 
    } 
} 

通知,我已經採取了所有文件從形式上傳的數據第一次,在foreach循環中運行你的代碼。

+0

謝謝你的回答,我做了你所做的,但是當我上傳多個圖片時出現這個錯誤'SQLSTATE [23000]:完整性約束違規:1062對於'UNIQ_EB95123F5E237E06''鍵重複輸入'the_name_of_the_form' –

+0

然後向我們展示實體類(請更新第一篇文章)。 –

+0

我使用實體'Restaurant'編輯了這個帖子(f是你期望的) –

1

您需要處理上傳方法中的數組,因爲它會從表單中獲取該數組,而不是單獨的UploadedFile對象。

/** 
* @param UploadedFile $file 
* @param $targetDir 
* @return array 
*/ 
public function upload($files, $targetDir) 
{ 
    if(!is_array($files)) { 
     $files = (array) $files; // cast to array in case of a form that isn't multiple. 
    } 
    $filenames = []; 
    foreach($files as $file) { 
     $filenames[] = md5(uniqid()).'.'.$file->guessExtension(); 
     $file->move($targetDir, $fileName); 
    } 

    return $filenames; 
} 

youll需要擺弄無論是獲得回報filename變量,因爲它現在會是一個數組。

另一種方式,在我看來是更好的用戶體驗,是使用CollectionType並呈現每個文件的文件輸入框。下面是一個簡單的例子。

$builder->add('file_uploads', CollectionType::class, [ 
     'entry_type' => FileType::class, 
     'entry_options' => [ 
      'label'  => 'Photos du restaurant', 
      'multiple'  => true, 
      'required'  => false, 
      'mapped'  => false, 
      'attr'   => [ 
       'accept' => '.jpg,.jpeg,.png', 
      ], 
      'allow_add' => true, 
      'allow_delete' => true, 
     ], 
     'prototype'  => true, 
    ]); 

然後你需要處理上傳處理程序中產生的ArrayCollection。但是這爲用戶提供了更好的界面。

我沒有測試過這個代碼片段,因此您可能需要調試或調整它以適應您的操作。

+0

感謝你的回答,我實際上在我的一個嘗試中使用了一個集合類型,我創建了一個'MediaType'形式並添加到我的' RestaurantType「與」CollectionType「屬性。但無法完成,因爲我的文件upoloader工作不正常'multiple_files'我也會嘗試你的方法 –

+0

我用你的第一個方法(你改變'UploadedFile'),當我創建一個新餐廳'警告:strrpos()期望參數1是字符串,數組給定' –