栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP多部分表单数据PUT请求?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP多部分表单数据PUT请求?

首先,

$_FILES
在处理PUT请求时不填充。它仅在处理POST请求时由PHP填充。

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

// Fetch content and determine boundary$raw_data = file_get_contents('php://input');$boundary = substr($raw_data, 0, strpos($raw_data, "rn"));// 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 == "--rn") break;    // Separate content from headers    $part = ltrim($part, "rn");    list($raw_headers, $body) = explode("rnrn", $part, 2);    // Parse the headers list    $raw_headers = explode("rn", $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
包含原始文件名(如果请求中提供了该文件名,并且适用于领域。

请注意,以上

multipart
内容仅适用于内容类型。
Content-Type
在使用上述内容解析正文之前,请务必检查请求标头。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号