I have trouble to read the file on Symfony 4. The file is read as a string, not as the File type. I understand this from this error response.
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
This line returns as
Call to a member function guessExtension() on string
But I set the types and everything perfectly :
my controller part for reading the values
$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
);
This is My CertificateType class
$builder->add('certificatePdf', FileType::class);
My Entity 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;
}
I follow the Symfony 4.1 Official document to create it. But I don't know why it is not working. Link for the Documentation