我尝试上传图片,但没有任何反应。
用户实体(注意:此实体包含有关用户信息的其他属性)
<?php
namespace Test\BackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* User
*
* @ORM\Table()
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class User implements UserInterface
{
...
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
private $path;
public $file;
...
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()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
return 'upload/files';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
$this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
UserType.php:
$builder
->add('firstName', 'text', array(
'required' => true
))
->add('lastName', 'text', array(
'required' => true
))
->add('email', 'email', array(
'required' => true
))
->add('job', 'text', array(
'required' => false
))
->add('file', 'file', array(
'label' => false,
'required' => false,
'mapped' => false
))
;
UserController.php
public function updateMyAccountAction($id, Request $request)
{
$entityManager = $this->get('doctrine')->getManager();
$user = $this->get('doctrine')
->getRepository('TestBackBundle:User')
->find($id);
if (!$user) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new UserType(), $user);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$entityManager->persist($user);
$entityManager->flush();
return $this->redirect($this->generateUrl('my_account', array('id' => $id)));
}
}
my_account.html.twig:
<form action="{{ path('my_account_update', { 'id': app.user.id }) }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<div class="btn-group">
<button type="submit" class="btn btn-info">Enregistrer</button>
</div>
</form>
当我提交表单时,我没有错误,但图片未上传。
编辑:
如果我从构建器中删除'mapped' => false
,则会出现此错误:
'Symfony \ Component \ HttpFoundation \ File \ UploadedFile'的序列化 不允许
如果我在验证后执行var_dump($user)
,则path attribute = NULL
答案 0 :(得分:0)
我可以提供简单的代码来上传文件的图像:)
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function upload()
{
if ($this->image instanceof UploadedFile && $this->image != null && $this->image->getRealPath()) {
$dir = "photos/";
$name = md5(rand().time()) . "." . $this->image->getClientOriginalExtension();
$this->image->move($dir, $name);
$this->setImage($dir.$name);
};
}