The file "test.jpeg" was not uploaded due to unknown error

0

I have configured the uploading of images correctly in the admin and followed this documentation: link

Now in another part of the system that does not use EasyAdminBundle I need to upload images, in the controller I have this:

public function crearequiposAction(Request $request) {
    $equipo = new Equipos();
    $form = $this->createForm(EquiposType::class, $equipo);

    $form->handleRequest($request);

    if ($form->isSubmitted()) {
        if ($form->isValid()) {

            $em = $this->getDoctrine()->getManager();
            $fecha=new \DateTime('now');

            $ruta = $equipo->getImageFile();
            $nombrep = $equipo->getLogo();
            $nombre = $nombrep.'.'.$ruta->guessExtension();

            $fileDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/images/equipos';

            $ruta->move(
                $fileDir,
                $nombre
            );


            $equipo->setLogo($nombre);
            $equipo->setUpdatedAt($fecha);


            $em->persist($equipo);
            $flush = $em->flush();

                if ($flush == null) {
                    $status = "Documento registrado correctamente";

                    $this->session->getFlashBag()->add("status", $status);
                    return $this->redirectToRoute("listado-torneos");

                } else {
                    $status = "No se registro equipo";
                }

        } else {
            $status = "No se registro equipo";
        }

        $this->session->getFlashBag()->add("status", $status);
    }

    return $this->render('AppBundle:Equipos:informacionequipos.html.twig', array(
                "form" => $form->createView()
    ));

}

The image uploads correctly but it shows me this error:

  

The file "test.jpeg" was not uploaded due to unknown error.

And I do not know how to solve this problem, any ideas?

Greetings

    
asked by juanitourquiza 18.10.2017 в 00:47
source

1 answer

1

This is the solution, the problem was in 2 parts:

First it is necessary to change the code of the form to this:

use Vich\UploaderBundle\Form\Type\VichFileType;
use Symfony\Component\HttpFoundation\File\File;

$builder
          ->add('nombre')
          ->add('grupo')
          ->add('numero')
          ->add('imageFile', VichFileType::class)
          ->add('torneos')
          ->add('save', SubmitType::class, array(
                     "attr" => array(
                     "class" => "save"
                )));

The second change is in the controller, when using the bundle it is not necessary to use the move line, this is only used when uploading images natively.

This is the driver code:

public function crearequiposAction(Request $request) {
        $equipo = new Equipos();
        $form = $this->createForm(EquiposType::class, $equipo);

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($equipo);
                $flush = $em->flush();
                    if ($flush == null) {
                        $status = "Equipo registrado correctamente";
                        $this->session->getFlashBag()->add("status", $status);
                        return $this->redirectToRoute("listado-torneos");
                    } else {
                        $status = "No se registro equipo";
                    }
            } else {
                $status = "No se registro equipo";
            }
            $this->session->getFlashBag()->add("status", $status);
        }
        return $this->render('AppBundle:Equipos:informacionequipos.html.twig', array(
                    "form" => $form->createView()
        ));
  }

Greetings

    
answered by 23.10.2017 / 04:34
source