2014-03-01 29 views
1

有人可以指出我在这里失踪了什么吗?我目前正在将文件上传添加到表单中,但文件不会从tmp目录中移出。我没有收到任何错误,但图像不会复制并位于tmp目录中。我已经尝试将权限设置到图像将驻留在文件夹中并修改getUploadDir()路径的权限,但仍然没有运气。Symfony - 文件上传的形式 - 文件不会从tmp目录移过

符号链接了web/bundles/bloggerblog。我认为问题在于我在getUploadDir中使用的路径,是否使用(src/blogger/blogbundle/Resources/public/images)中的路径,以便它符号链接或使用(web/bundles/bloggerblog/images)文件夹?

当前代码:

实体

protected $file; 

public function getUploadDir() 
{ 

    return '/bundles/bloggerblog/images'; 

} 

public function getUploadRootDir() 
{ 
    return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
} 

public function getWebPath() 
{ 
    return null === $this->image ? null : $this->getUploadDir().'/'.$this->image; 
} 

public function getAbsolutePath() 
{ 
    return null === $this->image ? null : $this->getUploadRootDir().'/'.$this->image; 
} 

/** 
* @ORM\PrePersist() 
* @ORM\PreUpdate() 
*/ 
public function preUpload() 
{ 
    if (null !== $this->file) { 
     // Generate a unique name 
     $this->image = uniqid().'.'.$this->file->guessExtension(); 
    } 
} 

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

    // If there is an error when removing 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->image); 

    unset($this->file); 
} 

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

控制器

public function createAction(Request $request) 
{ 
    $blog = new Blog(); 

    $form = $this->createFormBuilder($blog) 
     ->add('title', 'text') 
     ->add('author', 'text') 
     ->add('blog', 'textarea') 
     ->add('image', 'file') 
     ->add('tags', 'text') 
     ->add('save', 'submit') 
     ->getForm(); 

    $form->handleRequest($request); 
    if ($form->isValid()) { 
     $em = $this->getDoctrine() 
      ->getManager(); 
     $em->persist($blog); 
     $em->flush(); 

     // return new Response('Blog was added successfully'); 
     return $this->redirect($this->generateUrl('BloggerBlogBundle_homepage')); 
    } 

    $build['form'] = $form->createView(); 

    return $this->render('BloggerBlogBundle:Blog:create.html.twig', $build); 
} 

的CreateForm

{% extends 'BloggerBlogBundle::layout.html.twig' %} 

{% block title %}Add Blog{% endblock %} 

{% block body %} 
<h1>Add Blog</h1> 
{% include 'BloggerBlogBundle:Blog:form.html.twig' with { 'form': form } %} 
{% endblock %} 

形式

<form action="{{ path('BloggerBlogBundle_blog_create') }}" method="post" {{ form_enctype(form) }} class="blogger"> 
{{ form_widget(form) }} 

+0

您是否已将'@ HasLifecyclecallbacks'注释添加到您的实体类?在Symfony2文件上传中查看我的[分步指南](http://stackoverflow.com/a/17955934/1847340)。 – ferdynator

+0

是的,我做到了。我想出了我的问题。它在createFormBuilder中。 – webdev

回答

2

的问题是在控制器的createFormBuilder。

而不是

->add('image', 'file') 

它需要的是

->add('file', 'file', array(
      'label' => 'Image', 
      'required' => false 
     )) 

然后一切工作就像一个魅力,很高兴在几个小时我花了寻找答案没有白费。