PHP多部分表单数据PUT请求? [英] PHP multipart form data PUT request?

查看:91
本文介绍了PHP多部分表单数据PUT请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个RESTful API。我在使用不同动词上传图片时遇到问题。

I'm writing a RESTful API. I'm having trouble with uploading images using the different verbs.

请考虑:

可以通过对URL的post / put / delete / get请求创建/修改/删除/查看。

I have an object which can be created/modified/deleted/viewed via a post/put/delete/get request to a URL. The request is multi part form when there is a file to upload, or application/xml when there's just text to process.

要处理与上传的文件相关联的图片上传对象我正在做类似于:

To handle the image uploads which are associated with the object I am doing something like:

    if(isset($_FILES['userfile'])) {
        $data = $this->image_model->upload_image();
        if($data['error']){
            $this->response(array('error' => $error['error']));
        }
        $xml_data = (array)simplexml_load_string( urldecode($_POST['xml']) );           
        $object = (array)$xml_data['object'];
    } else {
        $object = $this->body('object');
    }

这里的主要问题是当尝试处理put请求时,$ _POST不包含put数据(据我所知!)。

The major problem here is when trying to handle a put request, obviously $_POST doesn't contain the put data (as far as I can tell!).

参考这是我如何构建请求:

For reference this is how I'm building the requests:

curl -F userfile=@./image.png -F xml="<xml><object>stuff to edit</object></xml>" 
  http://example.com/object -X PUT

有没有任何想法如何访问我的PUT请求中的 xml 变量?

Does anyone have any ideas how I can access the xml variable in my PUT request?

推荐答案

首先,处理PUT请求时,未填充 $ _ FILES 。它只在处理POST请求时由PHP填充。

First of all, $_FILES is not populated when handling PUT requests. It is only populated by PHP when handling POST requests.

您需要手动解析。这也适用于常规字段:

You need to parse it manually. That goes for "regular" fields as well:

// Fetch content and determine boundary
$raw_data = file_get_contents('php://input');
$boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));

// Fetch each part
$parts = array_slice(explode($boundary, $raw_data), 1);
$data = array();

foreach ($parts as $part) {
    // If this is the last part, break
    if ($part == "--\r\n") break; 

    // Separate content from headers
    $part = ltrim($part, "\r\n");
    list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);

    // Parse the headers list
    $raw_headers = explode("\r\n", $raw_headers);
    $headers = array();
    foreach ($raw_headers as $header) {
        list($name, $value) = explode(':', $header);
        $headers[strtolower($name)] = ltrim($value, ' '); 
    } 

    // Parse the Content-Disposition to get the field name, etc.
    if (isset($headers['content-disposition'])) {
        $filename = null;
        preg_match(
            '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', 
            $headers['content-disposition'], 
            $matches
        );
        list(, $type, $name) = $matches;
        isset($matches[4]) and $filename = $matches[4]; 

        // handle your fields here
        switch ($name) {
            // this is a file upload
            case 'userfile':
                 file_put_contents($filename, $body);
                 break;

            // default for all other files is to populate $data
            default: 
                 $data[$name] = substr($body, 0, strlen($body) - 2);
                 break;
        } 
    }

}

迭代, $ data 数组将填充您的参数, $ headers 数组将填充头对于每个部分(例如: Content-Type 等)和 $ filename 将包含原始文件名,if

At each iteration, the $data array will be populated with your parameters, and the $headers array will be populated with the headers for each part (e.g.: Content-Type, etc.), and $filename will contain the original filename, if supplied in the request and is applicable to the field.

请注意,上述操作仅适用于 multipart 内容类型。请确保在使用上述方法解析正文之前检查请求 Content-Type

Take note the above will only work for multipart content types. Make sure to check the request Content-Type header before using the above to parse the body.

这篇关于PHP多部分表单数据PUT请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆