How to upload to Silex?

Asked

Viewed 204 times

2

I did some research to know how to upload files in Silex Framework, but I found nothing about it.

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

  • 1

    This helps ? http://silex.sensiolabs.org/doc/2.0/providers/form.html

  • 1

    Um, this is the Formbuilder from Symfony. The form I don’t need to assemble, I’m ready. I just really need to upload.

  • Actually, I think I was floating. I think the answer is simple: Just make the same upload you do on Symfony (which is almost the same thing as Laravel)

  • Silex is not micro-framework? Wouldn’t it be better to use pure PHP to upload? https://secure.php.net/manual/en/features.file-upload.post-method.php

  • He uses the core of symfony... If already ready, I do not do on hand no.

1 answer

2


The Silex uses the components of Symfony. Therefore, it is possible to use the Symfony Request in order to upload.

Example:

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

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

});

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

Then, you can use the methods of this file to do the desired operations.

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

Behold:

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

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

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

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

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

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.