2

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 Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "/tmp/phpr2tM6D" does not exist" at [...]/vendor/symfony/http-foundation/File/File.php line 37 {"exception":"[object] (Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException(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'])]])  
4
  • What have you tried to debug the problem? Without seing any of your code, it's close to impossible to provide help Commented May 4, 2020 at 9:29
  • My first guess would be what @RonvanderHeijden suggested... Commented May 4, 2020 at 9:48
  • @RonvanderHeijden I have checked everything and these parameters are set at about ~500M, but my files have ~2M... Plus, I have EasyAdminBundle with VichUploader, and everything there works good Commented May 4, 2020 at 9:55
  • If you said files are uploaded correctly to your directory, please could you try to give us the complete code of your controller ? Commented May 5, 2020 at 7:03

2 Answers 2

1

First i would use your try+catch block to output your error details with

} catch(FileException $e) {
    dd($e);
}

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it does not even go into catch(), it throws that error (I know that xampp is not the best option, but at the moment I can run debug only here):
and I think that all privileges are ok
0
+50

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

2 Comments

I think problem is here: return $this->render('contact/warranty.html.twig', [ 'warranty_form' => $form->createView() ]);
I think problem is here: You are create form view with params with was add by "$form->handleRequest($request);". And it have reference to file wich was already moved. Probably after form is handled you should redirect user somewhere or reset form state. EX ``` lang-js // if form submitted and valid to do stuff and redirect if($form->isSubmitted() && $form->isValid()) { // do save file return new RedirectResponse('main page link'); } // else just render return $this->render('contact/warranty.html.twig', [ 'warranty_form' => $form->createView() ```

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.