我尝试使用Symfony2上传文件,但是当我没有收到任何错误或在日志中时,上传的文件都没有在服务器上创建。我正在使用虚拟小文本文件测试上传。
以下是文档实体的代码from the cookbook:
<?php
namespace Crm\EntiteBundle\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 Doc
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
public $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
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/docs';
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
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->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
$file = $this->getAbsolutePath();
if ($file) {
unlink($file);
}
}
}
这是我的(测试)控制器:
public function uploadAction(Request $request)
{
$document = new Doc();
$form = $this->createFormBuilder($document)
->add('name')
->add('file', 'file')
->add('submit','submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($document);
$em->flush();
return new Response();
}
return $this->render('CrmEntiteBundle:Form:upload.html.twig', array(
'form' => $form->createView(),
));
}
uploadAction的路线:
crmentite_upload:
path: /upload
defaults: { _controller: CrmEntiteBundle:Entite:upload }
这是(简单)Twig:
<form {{ form_enctype(form) }} action="{{ path('crmentite_upload') }}" method="POST">
{{ form_rest(form) }}
</form>
以下是生成的HTML:
<form enctype="multipart/form-data" action="/web/app_dev.php/upload" method="POST">
<div class="form-group">
<span class="required" title="Ce champ est requis">*</span>
<label class="control-label required" for="form_name">Name</label>
<input type="text" id="form_name" name="form[name]" required="required" maxlength="255" class="form-control" />
</div><div class="form-group">
<span class="required" title="Ce champ est requis">*</span>
<label class="control-label required" for="form_file">File</label>
<div class="form-group"><div class="input-group"><span class="input-group-btn"><span class="btn btn-primary btn-file"><span class="glyphicon glyphicon-folder-open"></span> Parcourir… <input type="file" multiple></span></span><input type="text" class="form-control" readonly></div></div><!--div class="input-group col-md-10"><span class="input-group-btn"><span class="btn btn-primary btn-file"><span class="glyphicon glyphicon-folder-open"></span> Parcourir…<input type="file" id="form_file" name="form[file]" required="required"></span><input type="text" class="form-control" readonly></span></div-->
</div><div><button type="submit" id="form_submit" name="form[submit]" class="btn btn">Submit</button></div> <input type="hidden" id="form__token" name="form[_token]" class="form-control" value="p_JPyYzTR2Es37Eocp_X2WkG0GtrJUJH9_ZvyuEj29U" />
</form>
有关信息,我之前创建了具有正确权限的uploads/docs
文件夹,并在doc表中添加了一条记录。
var_dump($this->getFile());exit();
函数中的upload
返回NULL
,与preUpload
函数相同。
$request->files
转储为空,为$_FILES
转储。
答案 0 :(得分:2)
在树枝上试试这个:
<form {{ form_enctype(form) }} action="{{ path('youruploadactionroutename') }}" method="POST">
{{ form_rest(form) }}
</form>
我认为它可能是未设置的enctype。 您可能想尝试放入表单构建器
->add('file', 'file')
输入字段没有表单的名称或ID,这是html中的错误,更具体地说是field.html.twig
:
{% block file_widget -%}
{% spaceless %}
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
<span class="glyphicon glyphicon-folder-open"></span> Parcourir… <input type="file" multiple>
</span>
</span>
<input type="text" class="form-control" readonly>
</div>
</div>
{% endspaceless %}
{% endblock file_widget %}
修正:
{# with error #}
{% block file_widget -%}
{% spaceless %}
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
<span class="glyphicon glyphicon-folder-open"></span> Parcourir… <input type="file" multiple {{ block('widget_attributes') }}>
</span>
</span>
<input type="text" class="form-control" readonly>
</div>
</div>
{% endspaceless %}
{% endblock file_widget %}