symfony文件上传错误,参数1传递给... null

时间:2017-10-11 06:18:09

标签: php symfony file-upload symfony-3.3

在Symfonys示例之后,我一直在尝试将文件上传到我的表单中:https://symfony.com/doc/current/controller/upload_file.html 可悲的是,我似乎没有让它发挥作用。 当我尝试使用表单上传图像时,显示此错误。

  

捕获致命错误:参数1传递给   AppBundle \ Service \ FileUploader :: upload()必须是。的实例   Symfony \ Component \ HttpFoundation \ File \ UploadedFile,null给定,调用   在/var/www/html/test/src/AppBundle/Controller/DefaultController.php中   第156行并定义

我的控制器中有以下内容:

<?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, FileUploader $fileUploader){
         //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());

        $users = new Users();
        //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 = $users->getBild();
             dump($file);
             $filename = $fileUploader->upload($file);
             $users->setBild($filename);

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

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

对于上传,我创建了一个名为FileUploader.php的服务

<?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;
     }
}

服务在services.yml中定义:

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

并且变量 file_directory 在config.yml中定义:

   parameters:
        locale: en
        file_directory: '/tmp'

我已经转储了 $ file 的值,并且在提交后它为空,因此函数 upload 不起作用,因为该文件没有值。对于任何建议,我真的感到困惑和感激。

2 个答案:

答案 0 :(得分:0)

在我的情况下,解决方案被添加到标签

enctype =“ multipart / form-data”

答案 1 :(得分:0)

运行内置服务器时遇到相同的问题。我的原因是php.ini上的post_max_size。大于最大大小的文件将作为null传递到控制器。

如果使用内置服务器,您可能还需要检查upload_max_filesize