【发布时间】:2017-10-11 06:18:08
【问题描述】:
在 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'
我已经dump了$file的值,提交后为null,所以upload函数不起作用,因为文件没有值。我真的很困惑,并感谢任何建议。
【问题讨论】:
-
添加
use Symfony\Component\HttpFoundation\File\UploadedFile; -
很遗憾似乎没有什么不同,但感谢您的尝试:)
-
我不太明白关于实例的部分在哪里,但上面的文档包括关于上传的所有内容,除了用户实体,但如果相关,我可以添加。跨度>
-
很可能 $file 不是
Symfony\Component\HttpFoundation\File\UploadedFile对象 -
感谢您我检查了 $file 的定义并找到了解决方案。非常感谢 :) 我不得不将
$file = $users->getBild();更改为$file = $form['bild']->getData();。
标签: php symfony file-upload symfony-3.3