1

I have a form with a fileupload and for this implementation I've followed the official documentation of symfony: When I try to save my form, I get the following error:

Notice: Undefined variable: fileUploader

It means that the following code from my controller has a problem in it...

<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use AppBundle\Service\FileUploader;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class DefaultController extends Controller {
/**
     * @Route("/bearbeiten/{id}", name="edit")
     */
     public function editAction($id, Request $request){
         //Daten aus der Datenbank mit $id
          $listen = $this->getDoctrine()
                ->getRepository('AppBundle:Users')
                ->find($id);
        //Variabeln vom passende Eintrag werden geholt und gesetzt
        $listen->setVorname($listen->getVorname());
        $listen->setNachname($listen->getNachname());
        $listen->setStrasse($listen->getStrasse());
        $listen->setOrt($listen->getOrt());
        $listen->setPLZ($listen->getPLZ());
        $listen->setBeschreibung($listen->getBeschreibung());
        $listen->setBild($listen->getBild());

        //Formular wird erstellt
        $form = $this->createFormBuilder($listen)
                ->add('vorname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('nachname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('strasse', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('ort', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('plz', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('beschreibung', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('bild', FileType::class, array('label'=>'Bild (JPEG-Datei)', 'data_class'=>null))
                ->add('save', SubmitType::class, array('label'=>'Speichern', 'attr'=>array('class'=>'btn btn-primary')))
                ->add('home', SubmitType::class, array('label'=>'zurück', 'attr'=>array('class'=>'btn btn-default')))
                ->getForm();

        $form->handleRequest($request);
        //Falls die Form valid ist....
        if($form->isSubmitted()){
            //Daten aus der Form in Variabeln sichern
             $vorname = $form['vorname']->getData();
             $nachname = $form['nachname']->getData();
             $strasse = $form['strasse']->getData();
             $ort = $form['ort']->getData();
             $plz = $form['plz']->getData();
             $beschreibung = $form['beschreibung']->getData();
             $file = $form['bild']->getData();

             **$filename = $fileUploader->upload($file);**

             $listen->setBild($filename);
             //Doctrine aktivieren
             $em=$this->getDoctrine()->getManager();
             $listen = $em->getRepository('AppBundle:Users');
             //Führt den Befehl in der DB aus
             $em->flush();

              if ($formsearch->get('home')->isClicked()){
                $textsa = 'Zurück geklickt';
                dump($textsa);
                return $this->redirectToRoute('homepage');
            }
             return $this->redirectToRoute('homepage');
         }

        return $this->render('main/edit.html.twig', array('listen'=>$listen, 'form'=>$form->createView())); 
     }
}

...on the line $filename = $fileUploader->upload($file);. Even though I've linked my Service with a USE-Statement. Below is my service:

<?php
namespace AppBundle\Service;

use Symfony\Component\HttpFoundation\File\UploadedFile;


class FileUploader
{
     private $targetDir;
     public function __construct($targetDir) {
         $this->targetDir = $targetDir;
     }
     public function upload(UploadedFile $file) {
         $fileName = md5(uniqid()).'.'.$file->guessExtension();
         $file->move($this->getTargetDir(), $fileName);
         return $fileName;
     }
     public function getTargetDir() {
         return $this->targetDir;
     }
}

And I've defined the service under services.yml:

AppBundle\Service\FileUploader:
   arguments:
      $targetDir: '%file_directory'

config.yml:

parameters:
    file_directory: '/tmp'

Does anyone know what I'm missing?

1 Answer 1

2

you need to pass FileUploader to the controller like this:

public function editAction($id, Request $request, FileUploader $fileUploader){

Or get the service into your controller like this:

$fileUploader = $this->get('your_service');

Or instantiate It into your code like this:

$fileUploader = new FileUploader();

If you read the documentation inside the action It pass the FileUploader :

Documentation

Sign up to request clarification or add additional context in comments.

4 Comments

well that was fast and helpful, now I get a different error but I can work with that, thanks a lot :)
Great to help you! Accept the answer if has helped you to solve the problem please @origins523
just have to wait for 8 minutes till it's working ;)
No problem, when you can :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.