【问题标题】:PATCH/PUT not accepting multipart/form-data file uploads?PATCH/PUT 不接受多部分/表单数据文件上传?
【发布时间】:2015-03-01 07:07:45
【问题描述】:

知道为什么 PATCH 和 PUT 不接受多部分/表单数据文件上传吗?

当我运行var_dump($_FILES) 时,它会输出array(0) { }。任何想法为什么会发生这种情况?如果我发布文件,它工作正常。

以下是我正在运行的请求的示例。

提前致谢!

PUT /test.php HTTP/1.1
Content-Type: multipart/form-data; boundary=__X_PAW_BOUNDARY__
Host: [redacted]
Connection: close
User-Agent: Paw/2.1.1 (Macintosh; OS X/10.10.2) GCDHTTPRequest
Content-Length: 17961

--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="avatar"; filename="default.png"
Content-Type: image/png

PNG


[IMAGE DATA]
--__X_PAW_BOUNDARY__--

【问题讨论】:

    标签: php post file-upload patch multipartform-data


    【解决方案1】:

    使用 PUT 请求上传文件时,您不使用 multipart/form-data。 PUT 请求与 GET 请求几乎相同。您应该做的就是将文件的内容放入请求的正文中。之后,您可以使用以下代码检索文件,如 php 文档中所述:

    http://php.net/manual/en/features.file-upload.put-method.php):

    <?php
    /* PUT data comes in on the stdin stream */
    $putdata = fopen("php://input", "r");
    
    /* Open a file for writing */
    $fp = fopen("myputfile.ext", "w");
    
    /* Read the data 1 KB at a time
       and write to the file */
    while ($data = fread($putdata, 1024))
      fwrite($fp, $data);
    
    /* Close the streams */
    fclose($fp);
    fclose($putdata);
    ?>
    

    【讨论】:

    • 我不知道如何从这个例子中只获取输入文件。我的表单一起提交输入和输入文件,它们都保存在file.ext中。如果我使用 post 方法,我可以访问来自 $_POST['name'] 的输入和来自 $_FILE['file']['name'] 的文件,但这......如何?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2014-08-18
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多