wluczykuj
wluczykuj

Reputation: 33

Upload file in Symfony 3.4 - Operation of update - how i should programming editAction?

i have a problem with my editAction(), It looks like it the problem is with path: "The file "G:\xampp5.6\htdocs\future\future/web/uploads/images/G:\xampp5.6\tmp\phpB0E6.tmp" does not exist" .
I dont know what can i do.

    /**
 * Displays a form to edit an existing blog entity.
 *
 * @Route("/{id}/edit", name="blog_edit")
 * @Method({"GET", "POST"})
 */
public function editAction(Request $request, Blog $blog)
{

    $blog->setImage(
new File($this->getParameter('image_directory').'/'.$blog->getImage()));

    $deleteForm = $this->createDeleteForm($blog);
    $editForm = $this->createForm('AppBundle\Form\BlogType', $blog);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {

        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('blog_edit', array('id' => $blog->getId()));
    }

    return $this->render('blog/edit.html.twig', array(
        'blog' => $blog,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

EDIT: How can I set correct paths? Because in my database path is G:\xampp5.6\tmp\phpA7BF.tmp and should only name image for example fa7bcdd50522b0592c5f98ab8313041.jpeg

Upvotes: 0

Views: 264

Answers (1)

wluczykuj
wluczykuj

Reputation: 33

solution:

in config.yml we have to use backslash:

parameters:
locale: en
image_directory: '%kernel.project_dir%\web\uploads\images'

BlogContoller.php

/**
 * Displays a form to edit an existing blog entity.
 *
 * @Route("/{id}/edit", name="blog_edit")
 * @Method({"GET", "POST"})
 */
public function editAction(Request $request, Blog $blog)
{



    $deleteForm = $this->createDeleteForm($blog);
    $editForm = $this->createForm('AppBundle\Form\BlogType', $blog);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {


        new File($blog->getImage());

        $file=$blog->getImage();
        $fileName=md5(uniqid()).'.'.$file->guessExtension();


        $file->move(
        $this->getParameter('image_directory'),$fileName
        );


        $blog->setImage($fileName);


        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('blog_edit', array('id' => $blog->getId()));
    }

    return $this->render('blog/edit.html.twig', array(
        'blog' => $blog,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

Upvotes: 1

Related Questions