I am trying to upload a file using Symfony 4 documentation (https://symfony.com/doc/4.0/controller/upload_file.html) – yes, I know that is an obsolete version, but at the moment I can’t update it.
I have done everything as in documentation (except for creating a new entity, because I only need to upload files, and send link to that file in email), files are uploaded correctly to my directory, but it throws HTTP 500 errors, and in log files there are something like this:
[2020-04-20 15:39:40] request.CRITICAL: Uncaught PHP Exception SymfonyComponentHttpFoundationFileExceptionFileNotFoundException: “The file “/tmp/phpr2tM6D” does not exist” at […]/vendor/symfony/http-foundation/File/File.php line 37 {“exception”:”[object] (SymfonyComponentHttpFoundationFileExceptionFileNotFoundException(code: 0): The file “/tmp/phpr2tM6D” does not exist at […]/vendor/symfony/http-foundation/File/File.php:37)”} []
any ideas?
ok, so form is basically just
{{ form_start(warranty_form) }}
{{ form_end(warranty_form)}}
and fragments of controller
$form = $this->createForm(WarrantyType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $documentFile */
$documentFile = $form->get('documentFile')->getData();
if($documentFile) {
$originalDocumentFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
$newDocumentFilename = uniqid().'.'.$documentFile->guessExtension();
try {
$documentFile->move('%kernel.project_dir%/public/uploads/pdf',$newDocumentFilename);
} catch(FileException $e) {
}
$message = (new Swift_Message('Test email '))
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody("a","text/plain");
}
and form is just a standard form, with possibility to upload PDF files
->add('documentFile', FileType::class, ['label' => 'Dokument', 'mapped' => false, 'required' => true, 'constraints' => [new File(['maxSize' => '1024k', 'mimeTypes' => ['application/pdf', 'application/x-pdf'], 'mimeTypesMessage' => 'Załaduj prawidłowy dokument'])]])
2
Answers
First i would use your try+catch block to output your error details with
There could be more information you need to debug.
From your provided error message “The file “/tmp/phpr2tM6D” does not exist” i would suggest to check that after an upload that this file or a similiar is created for “/tmp” AND way more important is that it can be accessed by your PHP Process. I would say the file is there, but it cannot be read by php. This could happen if your webserver and php use different owner/groups.
Can you show whole controller method? Do you use
$documentFile
variable after move file?It looks like the
catch(FileException $e)
should suppress your exception, and as it still appear I think maybe you somewhere try to access$documentFile
variable again or maybe tried to call$form->get('documentFile')->getData()
?You should know that
$documentFile->move('', '');
is use$renamed = rename($this->getPathname(), $target);
more see