我正在使用PHP运行一个webservice。其中一个网址需要从邮局的身体上得到几张照片。我目前使用fopen/fread解决方案:
$size1 = intval($_REQUEST['img1']);
$size2 = intval($_REQUEST['img2']);
$sizeToRead = 4096;
$datas1 = null;
$datas2 = null;
$h = fopen('php://input','r+');
while($size1 > 0) {
if($sizeToRead > $size1)
$sizeToRead = $size1;
$datas1 .= fread($h,$sizeToRead);
$size1 -= $sizeToRead;
}
fclose($h);
file_put_contents('received1.png',$datas1);
//Same thing for the second image这个解决方案工作得很好,但是我试图通过使用file_get_contents()来提高它的可读性:
file_put_contents('received1.png',file_get_contents('php://input',false,null,0,$size1));但这给了我一个错误
file_get_contents()流不支持搜索 file_get_contents()未能在流中定位21694
这能在小溪里找到吗?或者把第三个参数改为别的什么?
如果没有,是否有办法使我的代码更优雅/高效?
谢谢。
发布于 2013-03-15 14:57:13
引用手册的话
注意:使用php://input打开的流只能读取一次;该流不支持查找操作。但是,根据SAPI实现,可能会打开另一个php://input流并重新启动读取。只有在保存了请求正文数据时,才有可能这样做。通常,POST请求就是这种情况,而不是其他请求方法,例如PUT或PROPFIND。
(我强调)
https://stackoverflow.com/questions/15435277
复制相似问题