php - Symfony 3 不会将文件转换为 UploadedFile

标签 php symfony

我正在学习本教程 http://symfony.com/doc/current/controller/upload_file.html但是 $file 没有被转换为 UploadedFile,它仍然是一个字符串:

var_dump($file);
/home/owner/Desktop/Workspace/Documentary/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php:103:string '/tmp/phpmmv1LP' (length=14)

Type error: Argument 1 passed to DW\DocumentaryBundle\Uploader\PosterUploader::upload() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, called in /home/owner/Desktop/Workspace/DocumentaryWIRE/src/DW/DocumentaryBundle/Controller/Admin/DocumentaryController.php on line 106

创建 Action :

     /**
     * @param Request $request
     * @return Response
     */
    public function createAction(Request $request)
    {
        $documentary = new Documentary();

        $form = $this->createForm(DocumentaryType::class, $documentary);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $file = $documentary->getPoster();

            $posterUploader = $this->getPosterUploader();
            $fileName = $posterUploader->upload($file);

            $documentary->setPoster($fileName);

            $documentaryService = $this->getDocumentaryService();
            $documentaryService->save($documentary);
        }

        return $this->render('DocumentaryBundle:Admin:create.html.twig', array(
            'form' => $form->createView(),
        ));
    }

查看

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

表格:

<?php

namespace DW\DocumentaryBundle\Form;

use DW\DocumentaryBundle\Entity\Documentary;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DocumentaryType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class)
            ->add('storyline', TextareaType::class)
            ->add('summary', TextareaType::class)
            ->add('year', IntegerType::class)
            ->add('length', IntegerType::class)
            ->add('status', TextType::class)
            ->add('views', IntegerType::class)
            ->add('shortUrl', TextType::class)
            ->add('videoId', TextType::class)
            ->add('videoSource', TextType::class)
            ->add('poster', FileType::class, [
                'label' => 'Poster'])
            ->add('category', EntityType::class, [
                'class' => 'CategoryBundle:Category',
                'choice_label' => 'name',
            ])
            ->add('submit', SubmitType::class);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Documentary::class,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return "documentary";
    }
}

Controller :

<?php

namespace DW\DocumentaryBundle\Uploader;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class PosterUploader
{
    /**
     * @var string
     */
    private $targetDir;

    /**
     * @param string $targetDir
     */
    public function __construct(string $targetDir)
    {
        $this->targetDir = $targetDir;
    }

    /**
     * @param UploadedFile $file
     * @return string
     */
    public function upload(UploadedFile $file)
    {
        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->targetDir, $fileName);

        return $fileName;
    }
}

最佳答案

文档没有错误!他们在示例中不使用类型提示:

// src/AppBundle/Entity/Product.php
public function setBrochure($brochure)
{
    $this->brochure = $brochure;

    return $this;
}

当您使用 string 类型提示时,UploadedFile::__toString() 将被调用! 所以检查你的实体。它应该是这样的:

public function setPoster($poster)
{
   // ...
}

关于php - Symfony 3 不会将文件转换为 UploadedFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079351/

相关文章:

php - 在 Symfony2 中计算百分比

php - 使用 Symfony 和 Doctrine 按月和年检索项目

php - 使用自定义表单 symfony 进行 Stripe 结帐

php - hash() 与 crypt() 函数比较

php - Plesk (Linux) 的自定义 .htaccess 404 页面

php - 为什么这个 PDO Insert 执行了两次?

php - RabbitMQ 等待多个队列完成

php - 用于简单键值组表的 Doctrine Entity Design

php - 在 Windows 命令提示符上显示 UTF-8 输出

symfony - 不可能在卡住的ParameterBag上调用set()