How to upload to Silex?

2

I did some research to learn how to upload files in Silex Framework, but I did not find anything about it.

Is there any way to do this through Silex? Or would I have to do with nail only with PHP?

    
asked by anonymous 17.01.2017 / 15:06

1 answer

2

Silex uses the Symfony components. Therefore, you can use Symfony Request to be able to upload.

Example:

$app->post('/user/upload', function (Request $request) use ($app) {

     $upload = $request->files->get('arquivo');

});

In the example above, if the file is sent, the $upload variable will contain an instance of UploadedFile , if only one file is sent.

So you can use the methods of this file to do the operations you want.

To move the upload file, you use the UploadedFile::move method.

See:

$upload->move($diretorio, $nome_do_arquivo);

If you do not enter $nome_do_arquivo , the file will get the name that came from the form.

You may want to check the mime of the file. For this you can use the UploadedFile::getMimeType() method.

Still complementing, if you want to know the name of the temporary upload file you should use UploadedFile::getRealPath() .

To know the name that came from the client you use UploadedFile::getClientOriginalName()

References:

17.01.2017 / 16:30