Baldinof / roadrunner-bundle

A RoadRunner worker integrated in your Symfony app
MIT License
270 stars 48 forks source link

UploadedFile: The file could not be uploaded #37

Closed treadmillian closed 3 years ago

treadmillian commented 3 years ago

Since upgrading to RoadRunner 2.0.4, I've noticed when attempting to upload a file to the server Symfony is reporting the error The file could not be uploaded (see; https://symfony.com/doc/current/reference/constraints/File.html#uploaderrormessage), but never got this with RR 1.9.2

When the form gets validated, like so;

        $form = $this->createForm(UploadF307CType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

I get this error. Has anyone else had this issue? Max file upload size is set to 32M and the file is only 115K.

Path: children[importTemplatePath].data Message: The file could not be uploaded. Invalid value: Symfony\Component\HttpFoundation\File\UploadedFile {#250 …}

Violation:

Symfony\Component\Validator\ConstraintViolation {#3507 ▼
  -message: "The file could not be uploaded."
  -messageTemplate: "The file could not be uploaded."
  -parameters: []
  -plural: null
  -root: Symfony\Component\Form\Form {#3179 ▼
    name: "upload_f307_c"
    type_class: "App\Form\UploadF307CType"
    data: App\Entity\WorkflowClaim {#2390 ▼
      -id: 3672
      -stage: Proxies\__CG__\App\Entity\WorkflowStage {#2459 …}
      -submittedBy: Proxies\__CG__\App\Entity\User {#2538 …}
      -dateSubmitted: DateTime @1617803076 {#2386 ▶}
      -invoice: Proxies\__CG__\App\Entity\TrainingProviderInvoice {#2492 …}
      -importTemplatePath: null
      -processClaimUploads: Doctrine\ORM\PersistentCollection {#2519 …}
      -disableListeners: false
      #createdAt: DateTime @1617803076 {#2387 ▶}
      #updatedAt: DateTime @1617803076 {#2388 ▶}
    }
  }
  -propertyPath: "children[importTemplatePath].data"
  -invalidValue: Symfony\Component\HttpFoundation\File\UploadedFile {#250 …}
  -constraint: Symfony\Component\Validator\Constraints\File {#3246 …}
  -code: "0"
  -cause: null
}

Contents of the POST request

+files: Symfony\Component\HttpFoundation\FileBag {#209 ▼
    #parameters: array:1 [▼
      "upload_f307_c" => array:1 [▼
        "importTemplatePath" => Symfony\Component\HttpFoundation\File\UploadedFile {#250 ▼
          -test: false
          -originalName: "F307C 1310884 2020-21 P12.xlsx"
          -mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          -error: 0
          path: "/tmp"
          filename: "upload522885814"
          basename: "upload522885814"
          pathname: "/tmp/upload522885814"
          extension: ""
          realPath: "/tmp/upload522885814"
          aTime: 2021-04-15 19:46:53
          mTime: 2021-04-15 19:46:53
          cTime: 2021-04-15 19:46:53
          inode: 1475428
          size: 114590
          perms: 0100600
          owner: 1000
          group: 1000
          type: "file"
          writable: true
          readable: true
          executable: false
          file: true
          dir: false
          link: false
        }
      ]
    ]
  }

If I watch -d the /tmp directory, I can see the file is uploaded to the server and then is immediately deleted.

-rw------- 1 app app 114590 Apr 15 21:42 upload522885814

treadmillian commented 3 years ago

Just a follow-up of an actual working simple example that fails. Both the above reported issue and this example work fine if I just use; symfony serve.

Controller

    /**
     * @Route("/upload", name="upload_example", methods={"GET", "POST"})
     */
    public function import(
        Request $request
    ): Response {
        $form = $this->createForm(UploadF307CType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            dump('Valid');
        }

        return $this->render('claim/upload-f307c.html.twig', [
            'form' => $form->createView(),
        ]);
    }

Form

<?php

declare(strict_types=1);

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class UploadF307CType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('importTemplatePath', FileType::class, [
                'label' => false,
                'mapped' => false,
                'required' => true,
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Upload',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}

Template

{% block body %}
    {{ form(form) }}
{% endblock %}
treadmillian commented 3 years ago

I can also confirm after downgrading the roadrunner-bundle to 1.x the file upload immediately worked.

Baldinof commented 3 years ago

Hi!

Thanks for the report!

Can you try with the changes in #38 ?

treadmillian commented 3 years ago

Hi,

I can confirm that fixed the issue perfectly.

Thank you!

Baldinof commented 3 years ago

I released 2.0.1 with the fix :)