2

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

1 Answer 1

4

Try to replace

$file = $certificate->getCertificatePdf();

with

$file = $form->get('certificatePdf')->getData();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.