php 无法在 ftp_fget 之后回显流

php can't echo stream after ftp_fget

通过 ftp_fget 从远程服务器拉取流后无法回显流,但如果我将其写入文件,它可以正常工作...

我想我需要类似 fclose 和 fopen = freopen 的东西? :D

我想需要从 cache/buffer 写入文件流..?

如果我在写入文件后关闭并再次打开它,我可以轻松回显它。

不工作

    function download (){
     $fs = fopen('php://temp', "w+");
     ftp_fget($this->connection,$fs,$this->targetdir . $name, FTP_BINARY);
     ftp_close($this->connection);
     return $fs;
    }

    echo stream_get_contents(download('file.xml'));

有点工作(文件包含文本,因为脚本执行后自动关闭)

    function download (){
     $fs = fopen('file.xml', "w+");
     ftp_fget($this->connection,$fs,$this->targetdir . $name, FTP_BINARY);
     ftp_close($this->connection);
     echo $fs
     return $fs;
    }

    echo stream_get_contents(download('file.xml')); //still doesn't work

解决方案

写入后需要将指针设置回开头

所需命令:fseek()

function test (){
    $fs = fopen('php://temp', "w+");
    ftp_fget($this->connection,$fs,$this->targetdir . $name, FTP_BINARY);
    ftp_close($this->connection);
    fseek($fs,0);
    return $fs;
}