Search code examples
phpsymfonyvichuploaderbundle

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


I have set up uploading images correctly in the admin and I followed this documentation: https://symfony.com/doc/current/bundles/EasyAdminBundle/integration/vichuploaderbundle.html

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 "prueba.jpeg" was not uploaded due to an unknown error.

introducir la descripción de la imagen aquí

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

regards


Solution

  • This is the solution, the problem was in two parts.

    First in the form it is necessary to change 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 no longer necessary to use the move, that is only used when images are uploaded natively

    This is the code of the controller:

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

    Regards