gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:26:10 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com