Symfony 4 文件上传不工作

Symfony 4 File Upload Not working

我在 Symfony 4 上读取文件时遇到问题。文件被读取为字符串,而不是文件类型。我从这个错误响应中了解到这一点。

$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();

这一行 returns 为

Call to a member function guessExtension() on string

但我完美地设置了类型和一切:

我的控制器部分用于读取值

$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
);

这是我的证书类型class

$builder->add('certificatePdf', FileType::class);

我的实体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

尝试替换

$file = $certificate->getCertificatePdf();

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