2

I try to upload a file in symfony form, but i get this error :Notice: Undefined index: file which mean it didn't recover the file.

My form :

<?php
namespace Polytech\SkillsBundle\Form\Rapport;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class FicheOccasionType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ues', EntityType::class,
                array(
                    'class' => 'Polytech\SkillsBundle\Entity\UE',
                    'attr' => array('class' => 'browser-default ue'),
                    'choice_label' => 'nom',
                    'label' => false,
                    'required' => false,
                    'placeholder' => 'Choisissez une UE'
                )
            )
            ->add('etudiants', EntityType::class,
                array(
                    'class' => 'Polytech\SkillsBundle\Entity\Utilisateur',
                    'attr' => array('class' => 'browser-default etudiants'),
                    'choice_label' => 'nom',
                    'label' => false,
                    'required' => false,
                    'placeholder' => 'Choisissez un utilisateur'
                )
            )
            ->add('file', FileType::class, array('label' => 'PDF File'))
            ->add('submit', HiddenType::class)
            ->add('export', ButtonType::class, array('label' => 'Exporter'))
            ->add('import', ButtonType::class, array('label' => 'Import'));

    }

    public function getName()
    {
        return 'fiche_occasion';
    }
}

and this is my controller (the important part ) :

 if($form->isSubmitted() && $form->isValid() ) {
        $data = $request->request->get('fiche_occasion');
        $use = $data['ues'];
        $etudiants = $data['etudiants'];
        $file = $data['file'];
        $filename = md5(uniqid()).'.'.$file->guessExtension();
        $action = $data['submit'];

How I can retrieve the file information for the purpose of storing it in the web and storing its path in the database

7
  • Where did the error occur? Commented Mar 23, 2017 at 23:43
  • I get the error when i click in import or export button but this is not the problem because i can get all the information in the two cases i can get the $etudiants information and the $use but not the file Commented Mar 23, 2017 at 23:45
  • in this line $file = $data['file']; Commented Mar 23, 2017 at 23:47
  • 1
    In the generated form, check if it is being set as a "POST" rather then "GET" and also make sure that the form has an enctype="multipart/form-data" property Commented Mar 23, 2017 at 23:51
  • 1
    also try getting the file like this $form->get('file') Commented Mar 23, 2017 at 23:52

2 Answers 2

1

Files are not stored in post or get vars. Files are stored in $request->files.

symfony.com/doc/current/components/http_foundation.html#accessing-request-data

Try with $file = $request->files['file']; instead.

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

1 Comment

but i don't understand why the type is not UploadedFile ?
0

Try to follow this link:

http://symfony.com/doc/current/controller/upload_file.html

which contains a good practice (also security) to create form with Upload File.

In your case try to use $ficheOccasion (your object....):

$ficheOccasion = new FicheOccasion(); // Your Entity Class (?)
$form = $this->createForm(FicheOccasionType::class, $ficheOccasion);

and Symfony do the job after this line in controller:

$form->handleRequest($request);

and after:

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

you probably have access to file content by:

$file = $ficheOccasion->getFile();

so you can do this:

$file->guessExtension();
$file->move( .... )

and on the end before persist the object to database:

$ficheOccasion->setFile($fileName); // your prepared filename...

3 Comments

Precisely I really need this way of working but I just have a question to start with symfony. The teaching unit that is an entity, studying it another entity ... and the evaluation sheet which is also another entity. Each student has several files and each file is for a single student. So $fiche = new Fiche(); // Your Entity Class (?) $form = $this->createForm(Fiche::class, $fiche); create only Fiche entity with the file ??
i posted the probleme here stackoverflow.com/questions/43001978/… with details
I tried to change ficheOccasion with FichePDF which is my entity , i get this error : Neither the property "ues" nor one of the methods "getUes()", "ues()", "isUes()", "hasUes()", "__get()" exist and have public access in class "Polytech\SkillsBundle\Entity\FichePDF".

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.