Thanks to visit codestin.com
Credit goes to github.com

Skip to content

file uploaded but $file->getErrorMessage(); throws error "was not uploaded due to unknown error" #28130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BernardA opened this issue Aug 4, 2018 · 7 comments

Comments

@BernardA
Copy link

BernardA commented Aug 4, 2018

Symfony version(s) affected: 4.0.4

Description

UploadedFile->getErrorMessage() says file "was not uploaded due to unknown error", while file was
actually uploaded and $_FILES['file']['error] shows no error.

How to reproduce

public function UserXtraAction(Request $request, ValidatorInterface $validator) {
    
    $is_logged = $this->isGranted('IS_AUTHENTICATED_FULLY');
    if ($is_logged) {
        $user = $this->getUser();
    }
    $em = $this->getDoctrine()->getManager();
    $repo = $em->getRepository(UserXtra::class);
    $userxtra = new UserXtra();

    $form = $this->createFormBuilder($userxtra)
        ->add('imgFile', FileType::class, array('label' => 'file'))
        ->add('save', SubmitType::class, array('label' => 'Create Task'))
        ->getForm();

    $form->handleRequest($request);
    $userxtra->setUser($user);
    if ($form->isSubmitted() && $form->isValid()) {         
        /** 
         * @var UploadedFile $file 
         * */
        $file = $form->get('imgFile')->getData();

        $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
        // moves the file to the directory where brochures are stored
        $this->validateFile($file);
        $file->move(
            $this->getParameter('user_image_directory'),
            $fileName
        );

        $userxtra->setImgFile($fileName);
        //$em->persist($userxtra);
        //$em->flush();

        return new JsonResponse(array(
            'status' => 'ok', 
            'is_logged' => $is_logged,
            'upload_info' => $this->upload_info,
        ));
    }
}

private function validateFile ($file) {
    $extension = $file->guessExtension();
    $size = $file->getSize();
    $errors = $file->getErrorMessage();
    $this->upload_info = [
        'extension' => $extension,
        'size' => $size,
        'errors' => $errors,
    ] ;
}

Possible Solution

Additional context

@xabbuh
Copy link
Member

xabbuh commented Aug 4, 2018

getErrorMessage() will always return a non-empty string. You need to check with isValid() first if there was an error at all. I am closing here as there does not seem to be a bug, but just a little misunderstanding.

@xabbuh xabbuh closed this as completed Aug 4, 2018
@BernardA
Copy link
Author

BernardA commented Aug 6, 2018

There's no misunderstanding. There's just poor documentation.

@nicolas-grekas
Copy link
Member

@BernardA would you mind sending a doc PR to improve this please?

@BernardA
Copy link
Author

BernardA commented Aug 6, 2018

@nicolas-grekas. I have no idea what a doc PR is. Can you elaborate?

In the meantime I believe I found another issue with UploadedFile.
Unless I am completely misunderstanding this, it would seem that that class is full of holes.

Also look at
symfony/symfony-docs#10145
and
symfony/symfony-docs#10143

Concerning the other issue I referred to above:

In attempting to verify xabbuh's comments above, I forced an exception by setting a low

upload_max_filesize 1M

I also upgraded Symfony to latest, ie, 4.1.3, just in case.

I was expecting to get a IniSizeFileException. Instead I got:

The file "" does not exist

on

$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();

That would seem to mean that, of course, the file upload was stopped by upload_max_filesize, without an error. Then when I attempted to get $file by using

$file = $form->get('imgFile')->getData();

it come back empty.

Note that I am using exactly the code from Symfony's cookbook , with the exception of line above, ie, $file = $form->get('imgFile')->getData();

@nicolas-grekas
Copy link
Member

A "doc PR" is a pull request to improve the documentation, on the symfony-doc repository:
https://github.com/symfony/symfony-docs/
That's how things improve, by contributions from people like you in your situation :)

@xabbuh
Copy link
Member

xabbuh commented Aug 6, 2018

@BernardA The issue you describe in #28130 (comment) looks like it is the same as #27524.

@BernardA
Copy link
Author

BernardA commented Aug 6, 2018

@nicolas-grekas. Before one can think about improving the documentation, one needs a code that works properly. We do not seem to be there yet.

@xabbuh . I believe the basic issue is the same as the one you referenced. Additionally, ss I mentioned on symfony/symfony-docs#10145, there are some wrong assumptions on the code and comments on that documentation.

By the way, I realized that wrong links appeared on my initial issue above. It should be
symfony/symfony-docs#10145
and
symfony/symfony-docs#10143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants