如何从PUT请求中读取内容并在变量php中获取文件内容
下面是我想修改的代码如何从PUT请求中读取内容并在变量php中获取文件内容,php,fopen,put,Php,Fopen,Put,下面是我想修改的代码 $input = fopen("php://input", "r"); $temp = tmpfile(); $realSize = stream_copy_to_stream($input, $temp); fclose($input); if ($realSize != $this->getSize()){ return false;
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
if ($realSize != $this->getSize()){
return false;
}
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
我想将内容保存到内存中,并将其以accross方式传输到其他服务器,而不将其保存在apache服务器上
当我试图输出内容时,我只看到资源id#5。任何建议、评论都将受到高度重视。谢谢你的代码打开了文件句柄,文件句柄本身并不是内容。要将内容放入变量中,只需像读取其他文件一样读取:
$put = file_get_contents('php://input');
您拥有的代码将打开文件句柄,而文件句柄本身不是内容。要将内容放入变量中,只需像读取其他文件一样读取:
$put = file_get_contents('php://input');
要获取流的内容,请执行以下操作:
rewind($temp); // rewind the stream to the beginning
$contents = stream_get_contents($temp);
var_dump($contents);
或者,使用@deceze提到的file\u get\u contents
更新 我注意到你也在打开磁盘上的临时文件。您可能需要考虑简化代码,如:
$put = stream_get_contents(STDIN); // STDIN is an open handle to php://input
if ($put) {
$target = fopen('/storage/put.txt', "w");
fwrite($target, $put);
fclose($target);
}
要获取流的内容,请执行以下操作:
rewind($temp); // rewind the stream to the beginning
$contents = stream_get_contents($temp);
var_dump($contents);
或者,使用@deceze提到的file\u get\u contents
更新 我注意到你也在打开磁盘上的临时文件。您可能需要考虑简化代码,如:
$put = stream_get_contents(STDIN); // STDIN is an open handle to php://input
if ($put) {
$target = fopen('/storage/put.txt', "w");
fwrite($target, $put);
fclose($target);
}