作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在制作一个 Symfony2 应用程序,它需要有一个多图像上传选项。我已经使用食谱条目上传了单个文件: How to handle File Uploads with Doctrine 效果很好。我已经为上传和删除实现了生命周期回调。
现在我需要把它变成一个多重上传系统。我也从 Stack Overflow 阅读了一些答案,但似乎没有任何效果。
堆栈溢出问题:
我现在有以下代码:
文件实体:
<?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);
}
}
}
文件 Controller :
<?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()
;
新表单 Twig 代码:
<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>
最佳答案
这是一个已知问题 as referenced on GitHub .
正如他们所说,您应该将 []
附加到模板中的 full_name
属性:
{{ form_widget(images_form.file, { 'full_name': images_form.file.get('full_name') ~ '[]' }) }}
关于php - Symfony2 中多个文件上传的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445408/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!