2012-05-04 11 views
13

मैं एक सिम्फनी 2 एप्लिकेशन बना रहा हूं जिसमें एकाधिक छवि अपलोड विकल्प होना आवश्यक है। How to handle File Uploads with Doctrine जो ठीक काम करता है: मैं रसोई की किताब प्रविष्टि का उपयोग कर एक फाइल अपलोड कर दिया है। मैं अपलोड करने और हटाने के लिए lifecyclecallbacks लागू किया है।Symfony2 में एकाधिक फ़ाइल अपलोड के साथ समस्याएं

अब मुझे इसे एक से अधिक अपलोड सिस्टम में बदलना होगा। मैं स्टैक ओवरफ़्लो से कुछ जवाब के साथ-साथ पढ़ लिया है, लेकिन कुछ भी काम करने के लिए लगता है।

स्टैक ओवरफ़्लो प्रश्न:

  1. Multiple file upload with Symfony2
  2. multiple file upload symfony 2

मैं पल में निम्न कोड:

फ़ाइल निकाय:

<?php 
namespace Webmuch\ProductBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 


/** 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class File 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    public $id; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    */ 
    public $path; 

    /** 
    * @Assert\File(maxSize="6000000") 
    */ 
    public $file = array(); 

    public function __construct() 
    { 

    } 

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

    /** 
    * Set path 
    * 
    * @param string $path 
    */ 
    public function setPath($path) 
    { 
     $this->path = $path; 
    } 

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


    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 when displaying uploaded doc/image in the view. 
     return 'uploads'; 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->file) { 
      // do whatever you want to generate a unique name 
      $this->path[] = uniqid().'.'.$this->file->guessExtension(); 
     } 
    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     if (null === $this->file) { 
      return; 
     } 

     // if there is an error when moving the file, an exception will 
     // be automatically thrown by move(). This will properly prevent 
     // the entity from being persisted to the database on error 
     $this->file->move($this->getUploadRootDir(), $this->path); 

     unset($this->file); 
    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() 
    { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 
} 

FileController:

<?php 

namespace Webmuch\ProductBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 

use Webmuch\ProductBundle\Entity\File; 


/** 
* File controller. 
* 
* @Route("/files") 
*/ 
class FileController extends Controller 
{ 
    /** 
    * Lists all File entities. 
    * 
    * @Route("/", name="file_upload") 
    * @Template() 
    */ 
    public function uploadAction() 
    { 
     $file = new File(); 
     $form = $this->createFormBuilder($file) 
      ->add('file','file',array(
        "attr" => array(
         "accept" => "image/*", 
         "multiple" => "multiple", 
        ) 
       )) 
      ->getForm() 
     ; 

     if ($this->getRequest()->getMethod() === 'POST') { 
      $form->bindRequest($this->getRequest()); 
       $em = $this->getDoctrine()->getEntityManager(); 

       $em->persist($file); 
       $em->flush(); 

       $this->redirect($this->generateUrl('file_upload')); 
     } 

     return array('form' => $form->createView()); 
    } 
} 

और upload.html.twig:

{% extends '::base.html.twig' %} 

{% block body %} 
<h1>Upload File</h1> 

<form action="#" method="post" {{ form_enctype(form) }}> 

    {{ form_widget(form.file) }} 

    <input type="submit" value="Upload" /> 
</form> 
{% endblock %} 

मैं क्या एक से अधिक फाइल अपलोड सिस्टम के रूप में यह काम करने के लिए क्या करने के लिए पता नहीं है । के रूप में वे ट्यूटोरियल मैं पालन किया है तो मैं याद कर सकते हैं क्या क्या कर रहा है से हैं मैं टिप्पणी नहीं रखा है।

अद्यतन:

नया प्रपत्र कोड:

$images_form = $this->createFormBuilder($file) 
    ->add('file', 'file', array(
      "attr" => array(
       "multiple" => "multiple", 
       "name" => "files[]", 
      ) 
     )) 
    ->getForm() 
; 

नया प्रपत्र टहनी कोड:

<form action="{{ path('file_upload') }}" method="post" {{ form_enctype(images_form) }}> 

    {{ form_label(images_form.file) }} 
    {{ form_errors(images_form.file) }} 
    {{ form_widget(images_form.file, { 'attr': {'name': 'files[]'} }) }} 

    {{ form_rest(images_form) }} 
    <input type="submit" /> 
</form> 
+0

इस समय इस के साथ क्या काम नहीं कर रहा है? – halfer

+0

उत्तर के लिए धन्यवाद। अगर मैं उदाहरण के लिए 5 फाइलों का चयन करता हूं, तो केवल अंतिम फ़ाइल अपलोड की जा रही है। –

+0

आह हाँ - आपके इनपुट नियंत्रण में एक व्यक्तिगत नाम होना चाहिए - क्योंकि इस समय इसका कोई नाम नहीं है, यह सभी नियंत्रणों के लिए डिफ़ॉल्ट का उपयोग करता है। – halfer

उत्तर

15

यह एक ज्ञात मुद्दा as referenced on GitHub है।

के रूप में वे कहते हैं कि आप अपने टेम्पलेट में full_name विशेषता को [] संलग्न करना होगा:

{{ form_widget(images_form.file, { 'full_name': images_form.file.get('full_name') ~ '[]' }) }} 
+4

सिम्फनी 2.5 में आप इसे प्राप्त कर सकते हैं: '{{form_widget (images_form.file, {'full_name': images_form.file.vars.full_name ~ '[]'})}} – ezpn

+0

बढ़िया! यह सही उत्तर है और इसे वैध के रूप में चिह्नित किया जाना चाहिए। – xger86x

+0

गीथब मुद्दा लिंक के लिए धन्यवाद। यह वास्तव में मेरी सत्यापन समस्या के साथ मेरी मदद की। मैंने एक उत्तर के रूप में एक और समाधान जोड़ा, जो पूरी तरह से काम करता है। – func0der

3

मैं हाल ही में एक ही समस्या थी, यहाँ सुझाव पीछा किया, लेकिन एक त्रुटि मिली क्योंकि सत्यापनकर्ता 'फ़ाइल' सरणियों को संभाल नहीं कर सकते हैं।

तो मुझे अपना खुद का सत्यापनकर्ता लिखना पड़ा, जो कई फाइलों को संभाल सकता है। इसके लिए, मैंने this tutorial from Symfony.com का पालन किया, इसमें सत्यापनकर्ता 'फ़ाइल' से कोड कॉपी/पेस्ट किया, इसे foreach लूप के साथ encapsulated और आवश्यकतानुसार चर बदल दिया।

यदि आप ऐसा करते हैं, तो आप इसे अपनी इकाई में $file के लिए उपयोग कर सकते हैं।

+4

क्या आप वह कोड साझा कर सकते हैं ताकि मैं एक नज़र देख सकूं? Thnx। एनोटेशन के संबंध में –

4

मुझे नहीं पता कि एनोटेशन सिंटैक्स के साथ यह संभव है या नहीं।इसलिए मैं इसे नियंत्रक

$images_form = $this->createFormBuilder($file) 
    ->add('file', 'file', array(
     'constraints' => array(
      new NotBlank(), // Makes sure it is filled at all. 
      new All(array(// Validates each an every entry in the array that is uploaded with the given constraints. 
       'constraints' => array(
        new File(array(
         'maxSize' => 6000000 
        )), 
       ), 
      )), 
     ), 
     'multiple' => TRUE, 
    )) 
    ->getForm(); 

में सादे PHP में लिखने जा रहा हूं यह सिम्फनी 2.4 के बाद पूरी तरह से काम करना चाहिए। इससे पहले आपको विशेषता attr कुंजी में पहले से ही डालना होगा।

जैसा कि मैंने कहा था, आपको यह काम एनोटेशन के साथ करना है। यह काम कर सकता है लेकिन यदि आपको इसे एक पंक्ति में रखना है तो कम पठनीय हो सकता है।

मज़े करें;)

+0

: इसे सिम्फनी वेबसाइट पर एक उदाहरण के रूप में दिया गया है। http://symfony.com/doc/current/reference/constraints/All.html 'वर्ग उपयोगकर्ता { /** * @Assert \ सभी ({ * @Assert \ NotBlank, * @Assert \ लंबाई (न्यूनतम = 5) *}) */ संरक्षित $ पसंदीदा रंग = सरणी(); } ' – nexana

संबंधित मुद्दे