Search code examples
phpcurllaravel-4put

Accessing file in PUT route Laravel


I am building a REST API endpoint which looks like this: (In Laravel 4.2)

PUT /upload/{name}

Here is the Route in routes.php which is able to route the PUT request.

Route::put('/upload/{name}', 'UploadController@upload')
->where('name', '[a-zA-Z0-9]+');

Now, the content of this request must contain the file to be saved. I am using the following curl request to do a preliminary test of the endpoint.

curl -X PUT localhost/upload/filename -F filedata=@test.file

And this is how, the data is being read in PHP:

$blob = file_get_contents('php://input', 'r' );

This is not giving me the actual file rather giving me the whole HTTP request as raw text (expected that). I even tried parse_str() but did not work.

What is the way to use curl in both CLI and PHP to send PUT requests and receive them subsequently in PHP? I have always struggled with this one request type.


Solution

  • As @lukasgeiter has correctly said in the comments above: Here is the solution:

        use Request;   // needed in Laravel 5  
        $request = Request::instance();    // fetch the request instance
        $content = $request->getContent();   // get the raw content
    

    I have verified the length of the content and it matches the file which was PUT.