Search code examples
phpsymfonyfosrestbundle

How to work with files through FOSRestBundle


I'm using FOS Rest Bundle to create a REST API where I'm suppose to be able to handle file upload. The problem is I don't know how to send and receive the files. My only experience with file transfer is with $_FILES of PHP and it does not help me a lot.

I though about encoding in Base64 and sending the file in JSON but since the file can be quite large, it's not a viable solution. As of now, I'm thinking about doing a request to send the metadata with json and another request for sending the file. I did not find any example on how to handle the file transfer as it seems everyone but me knows how it works.

I'm also quite new to symfony2 but I understand the basic concepts.


Solution

  • FOSRestBundle or not, you can use the $files provided by Request to get the file/s that was uploaded.

    use Symfony\Component\HttpFoundation\Request;
    
    public function uploadAction(Request $request)
    {
    
        $file = $request->files->get('file');
        //do what ever you want to do with the files
    
    }
    

    if you want to do it using helpers offered by FOSRestBundle, check this https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1097