I am a beginner in symfony, I wanted to make a form to insert and store files in the database, I created my form, and here is my controller, and the services.yaml in which there is the path to where store the files, I would like to know what is missing so that the files can be saved in the database.
<?php
class ProductController extends AbstractController
{
/**
* @Route("/product/new", name="app_product_new")
*/
public function new(Request $request)
{
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $brochureFile */
$brochureFile = $form->get('brochure')->getData();
// this condition is needed because the 'brochure' field is not required
// so the PDF file must be processed only when a file is uploaded
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
$product->setBrochureFilename($newFilename);
}
// ... persist the $product variable or any other work
$this->entityManager->persist($product);
$this->entityManager->flush();
return $this->redirect($this->generateUrl('app_product_list'));
}
return $this->render('product/new.html.twig', [
'form' => $form->createView(),
]);
}
}
?>