1

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) }}

2
  • Have you added the @HasLifecyclecallbacks annotation to your entity class? Check out my Step-by-step guide on Symfony2 file upload.
    – ferdynator
    Mar 1, 2014 at 23:56
  • Yes, I did. I figured out my problem. It was in the createFormBuilder.
    – webdev
    Mar 2, 2014 at 0:06

1 Answer 1

2

The problem was in the createFormBuilder in the controller.

Instead of

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

It needed to be

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

Then everything worked like a charm, glad the few hrs I spent searching for an answer wasn't in vain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.