Can someone point out what I'm missing here? I'm currently adding in file uploads into a form yet the file does not move over from the tmp directory. I'm not getting any errors but the image does not copy over and is residing in the tmp directory. I've tried setting permissions to the file folders where the images will reside and tinkering with the getUploadDir() path but still no luck.
The web/bundles/bloggerblog is symlinked. I think the problem lies in the path I am using in the getUploadDir, do I use the path from (src/blogger/blogbundle/Resources/public/images) so it's symlinked or use the (web/bundles/bloggerblog/images) folder?
Current code:
entity
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);
}
}
controller
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
<form action="{{ path('BloggerBlogBundle_blog_create') }}" method="post" {{ form_enctype(form) }} class="blogger">
{{ form_widget(form) }}