मैं एक सिम्फनी 2 एप्लिकेशन बना रहा हूं जिसमें एकाधिक छवि अपलोड विकल्प होना आवश्यक है। How to handle File Uploads with Doctrine जो ठीक काम करता है: मैं रसोई की किताब प्रविष्टि का उपयोग कर एक फाइल अपलोड कर दिया है। मैं अपलोड करने और हटाने के लिए lifecyclecallbacks लागू किया है।Symfony2 में एकाधिक फ़ाइल अपलोड के साथ समस्याएं
अब मुझे इसे एक से अधिक अपलोड सिस्टम में बदलना होगा। मैं स्टैक ओवरफ़्लो से कुछ जवाब के साथ-साथ पढ़ लिया है, लेकिन कुछ भी काम करने के लिए लगता है।
स्टैक ओवरफ़्लो प्रश्न:
मैं पल में निम्न कोड:
फ़ाइल निकाय:
<?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>
इस समय इस के साथ क्या काम नहीं कर रहा है? – halfer
उत्तर के लिए धन्यवाद। अगर मैं उदाहरण के लिए 5 फाइलों का चयन करता हूं, तो केवल अंतिम फ़ाइल अपलोड की जा रही है। –
आह हाँ - आपके इनपुट नियंत्रण में एक व्यक्तिगत नाम होना चाहिए - क्योंकि इस समय इसका कोई नाम नहीं है, यह सभी नियंत्रणों के लिए डिफ़ॉल्ट का उपयोग करता है। – halfer