doujuegai8830 2015-02-05 14:49
浏览 118
已采纳

在PUT路由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.

  • 写回答

1条回答 默认 最新

  • doumao1047 2015-03-16 14:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作