doukai2839 2019-02-27 06:29
浏览 108

Slim框架文件上传适用于POST,不适用于PUT

I'm trying to upload a file to my Slim Framework server using PUT. I've successfully created a POST route where I can retrieve the uploaded file with no problem, but if I send the same request to the same route, with the only difference being the method, I receive a file from POST, null from PUT.

Here's my router:

            $app->put('/users/{id}', 'RideTimeServer\API\Controllers\UserController:update');
            $app->post('/users/{id}', 'RideTimeServer\API\Controllers\UserController:update'); // DEBUG:

And then the request is just passed through controller to processPicture method:

    protected function processPicture(Request $request): string
    {
        // First look for an uploaded picture
        if (!empty($request->getUploadedFiles()['picture'])) {
            // http://www.slimframework.com/docs/v3/cookbook/uploading-files.html
            $uploadedFile = $request->getUploadedFiles()['picture'];
            /**
             * { file, name, type }
             */
            var_dump($uploadedFile);
        // Then check URL
        } elseif (!empty($request->getParsedBody()['picture'])) {
            $url = $request->getParsedBody()['picture'];
            if (!filter_var($url, FILTER_VALIDATE_URL)) {
                throw new UserException('Invalid picture URL', 400);
            }
            var_dump($url);
        } else {
            // $this->container['logger']->addInfo('Submitted user with no picture');
            var_dump('Submitted user with no picture');
            var_dump($request->getUploadedFiles());
            var_dump($request->getParsedBody());
        }
        return '';
    }

Now if I send

curl -X PUT \
  http://localhost/ridetime/api/users/1 \
  -H 'Authorization: Bearer t0k3n' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'picture=@/home/asdf/Tmp/Screenshot_20190125_005049.png'

i receive Submitted user with no picture, [] and null from the var_dump, yet if I change the request to POST:

curl -X POST \
  http://localhost/ridetime/api/users/1 \
  -H 'Authorization: Bearer t0k3n' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'picture=@/home/asdf/Tmp/Screenshot_20190125_005049.png'

I get details of the uploaded file.

I tried replacing the -F 'picture=@...png with --upload-file ./file.png and the result was the same.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?