从获取标头信息php://input.


Get header info from php://input

我有一个来自设备的文件(图像),通过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当我打印它时,我明白它不是put请求的头,而是页面的头。不是我需要的。

您可以尝试使用getallheaders()函数,该函数的唯一目的是检索请求头:

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

请注意,最好对键进行预处理,以处理诸如Content-name之类的标头(请注意-附近字母大小写的变化)。