我有一个来自设备的文件(图像),通过 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/