首先是错误: 在字符串
上调用成员函数guessExtension()当我转储($ file) 它显示: “C:\ XAMPP \ TMP \ php17A8.tmp”
public function indexAction(Request $request)
{
$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();
dump($file);
$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');
}
return $this->render('image/image.html.twig', array(
'imageform' => $form->createView()
));
}
形式:
{
$builder
->add('title', TextType::class)
->add('description', TextType::class)
->add('image', FileType::class, array('label'=>'Upload Image'))
->add('submit', SubmitType::class)
;
}
我已经做了几乎所有的事情,但我仍然收到错误..我在doc中也做了同样但仍然一样......
实际上我按照他手动创建这个类的教程但是我使用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;
}
}
答案 0 :(得分:0)
您必须从实体中的setter和getter方法中删除类型。
public function getImage()
{
return $this->image;
}
public function setImage($image): self
{
$this->image = $image;
return $this;
}
如果为这两个方法添加一个类型,将调用SplFileInfo中的__toString方法,因为UploadedFile扩展了File和File扩展了SplFileInfo。