Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PUT请求中读取内容并在变量php中获取文件内容_Php_Fopen_Put - Fatal编程技术网

如何从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);
}