0

I've been following an article by Fabien Potencier about building your own framework from Symfony Components.

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

I thought that the Request object could be used as a replacement for the PHP superglobals. However if I upload a file, and it encounters errors (i.e. empty file upload), it doesn't contain any error information?

Here is the output of the $_FILES array, it contains the error code

Array
(
    [inputIndex] => Array
        (
            [name] => Array
                (
                    [3] => 
                )

            [type] => Array
                (
                    [3] => 
                )

            [tmp_name] => Array
                (
                    [3] => 
                )

            [error] => Array
                (
                    [3] => 4
                )

            [size] => Array
                (
                    [3] => 0
                )

        )

)

Here's the output of the $request object

Array
(
    [inputIndex] => Array
        (
            [3] => 
        )

)

In the case of $_FILES the request object loses a lot of information. Is it contained anywhere else in the request object? How does Symfony handle this?

Andrew Berridge
  • 451
  • 5
  • 15

1 Answers1

2

The Request object's purpose is to hold the request data, not to validate it. If you want to make validation, create an object (an Entity if you save request data somewhere or just a simple php object if not), set the content of the $_FILES to the object, like:

$model->setFile($request->files->get('fileFormInputName'));

and use the validator component to validate the file using the File constraint. If there is an attachment sent, the $request->files->get('fileFormInputName') will be instanceof Symfony\Component\HttpFoundation\File\UploadedFile.

I hope this answers you question.

zeykzso
  • 493
  • 2
  • 9