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.

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

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄