我无法在Symfony 4上读取文件。文件读取为字符串,而不是文件类型。我从此错误响应中了解了这一点。
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
此行返回
在字符串上调用成员函数guessExtension()
但我完美地设置了类型和所有内容:
$certificate = new Certificate();
$form = $this->createForm(CertificateType::class, $certificate);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $file stores the uploaded PDF file
/**
* @var UploadedFile $file
*/
$file = $certificate->getCertificatePdf();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
// moves the file to the directory where brochures are stored
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
$builder->add('certificatePdf', FileType::class);
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Please, upload the verified certificate as a PDF file.")
* @Assert\File(mimeTypes={ "application/pdf" })
*/
private $certificatePdf;
public function getCertificatePdf()
{
return $this->certificatePdf;
}
public function setCertificatePdf(string $certificatePdf): self
{
$this->certificatePdf = $certificatePdf;
return $this;
}
我遵循Symfony 4.1官方文档进行创建。但是我不知道为什么它不起作用。 Link for the Documentation
答案 0 :(得分:4)
尝试更换
$file = $certificate->getCertificatePdf();
使用
$file = $form->get('certificatePdf')->getData();