symfony - 对字符串调用成员函数guessExtension()

标签 symfony symfony4

在 Symfony 4 中我收到此错误: 对字符串调用成员函数guessExtension() 在之前,我使用相同的代码来上传图像并且做得很好,但在这里我遇到了错误。 有人遇到同样的问题并解决了吗?

$em = $this->getDoctrine()->getManager();
    $imageEn = new Image();

     $form = $this->createForm(ImageUploadType::class, $imageEn);

     $form->handleRequest($request);

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


     /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */

        $file = $imageEn->getImage();

        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->getParameter('image_directory'), $fileName);

        $imageEn->setImage($fileName);
        $em->persist($imageEn);
        $em->flush();

        $this->addFlash('notice', 'Post Submitted Successfully!!!');

        return $this->redirectToRoute('image_upload');
     }

表格:

  {
    $builder
        ->add('title', TextType::class)
        ->add('description', TextType::class)
        ->add('image', FileType::class, array('label'=>'Upload Image'))
        ->add('submit', SubmitType::class)
    ;
}

图像类别:

实际上,我按照教程手动创建了这个类,但我使用 Command 来创建类,我将他的所有代码与我的代码进行了比较,结果是正确的。

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
*/
class Image
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $title;

/**
 * @ORM\Column(type="string", length=255)
 */
private $description;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank(message="Please upload image")
 * @Assert\File(mimeTypes={"image/jpeg"})
 */
private $image;

public function getId()
{
    return $this->id;
}

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

public function getDescription(): ?string
{
    return $this->description;
}

public function setDescription(string $description): self
{
    $this->description = $description;

    return $this;
}

public function getImage(): ?string
{
    return $this->image;
}

public function setImage(string $image): self
{
    $this->image = $image;

    return $this;
}

}

最佳答案

替换

$file = $imageEn->getImage()

$file = $form->get('image')->getData();

关于symfony - 对字符串调用成员函数guessExtension(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604601/

相关文章:

php - 模拟 Controller 从 WebTestCase 调用的服务

symfony - 我如何知道 SonataAdminBundle 中 ListMapper->add() 的第三个参数有哪些选项可用

Symfony 2 Assets 错误路径与 use_controllers : false

fosuserbundle - Symfony4 覆盖 FosUserBundle 模板

php - Doctrine QueryBuilder 按字段优先级排序结果

symfony - Twig:渲染 Symfony2 Controller ,扩展父模板中的 block

symfony - 设置 easyadmin_autocomplete 默认选择 Null。交响乐团

Symfony 4 服务于不同环境中的本地绑定(bind)

php - Api-Platform 上的 Data Persister 之后叫什么?

phpunit - 如何在 phpunit 测试中伪造 DateTime?