php - 从 php ://input 获取 header 信息

标签 php

我有一个来自设备的文件(图像),通过 put 发送

PUT /r.php HTTP/1.1
User-Agent: PHS/2.0.6
Host: localhost
Accept: */*
Content-Name: cam20141020084031.jpg,10001019
Content-Length: 35183
Expect: 100-continue

这就是我获取发送的图片的方式:

$res = file_get_contents("php://input");

$file = fopen('1.jpg', "w");
fputs($file, $res);
fclose($file);

我也需要单独获取内容名称。我找不到任何地方我怎样才能得到它。有人可以帮忙吗?

更新

$res = file_get_contents("php://input");
$vars=parse_str($res,$post_vars);
$headers = getallheaders();
$contentName=$headers['Content-Name'])
$file = fopen('1.jpg', "w");
$fileVar= fopen('1.txt', "w");
fputs($file, $res);
fputs($fileVar,$res);
fclose($fileVar);
fclose($file);

奇怪,但是这段代码似乎会永远加载。

更新1 当我 print_r 时,我知道它不是放置请求的标题,而是页面的标题。不是我需要的。

最佳答案

您可以尝试使用getallheaders()函数,其存在的唯一目的是检索请求 header :

$headers = getallheaders();
var_dump($headers['Content-Name']);

请注意,最好对键进行预处理以处理 Content-name 等 header (请注意 - 附近字母大小写的变化)。

关于php - 从 php ://input 获取 header 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462636/

相关文章:

c# - 将 C#(前端)连接到 apache/php/python(后端)

PHP:创建文本和图像列表显示图像重叠文本

javascript - AJAX 教程不工作

php - Wordpress:用png图像更改图标[进入功能]

php - Wordpress 菜单项未显示在菜单位置?

php - 有没有其他选择去死?

php - 解释一下这个XSS字符串,它使用了perl

javascript - JS 表单验证不适用于 html 表单

php - Javascript 中的真正随机字符串异步

php - Eclipse PDT 和 xDebug 中的交互式控制台