One line in the form type.
Change the type.
Keep the rest.
UploadType returns an UploadedFile that extends the HttpFoundation
class. handleRequest(), isValid(), typed properties and
move() work as they do with FileType.
$builder
->add('title', TextType::class)
->add('document', FileType::class, [
->add('document', UploadType::class, [
'constraints' => [new File(maxSize: '5M')],
])
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $document */
$document = $form->get('document')->getData();
$document->move($directory, $document->getClientOriginalName());
}
Choose a PDF, then save with an empty title. The title needs correcting. The document is still there.
PDF only, up to 1 MB. This public server destroys the file as soon as it arrives: the form keeps its reference.