How can I fetch a remote file in php over ssh and return file directly to the browser response without creating a copy of the file on the webserver

后端 未结 2 1305
野趣味
野趣味 2021-01-14 04:38

I am currently using a similar version of the below code to transfer a file from a remote server to my web server and then redirecting to the web server copy of the file in

2条回答
  •  借酒劲吻你
    2021-01-14 04:57

    You can use ssh2_sftp http://php.net/manual/en/function.ssh2-sftp.php ... you must install ssh2 bindings as PECL extension (http://php.net/manual/es/book.ssh2.php)

    An example code may be ...

    $sftp = ssh2_sftp($connection);
    
    $remote = fopen("ssh2.sftp://$sftp/path/to/file", 'rb');
    
    header( 'Content-type: ......');
    
    while(!feof($remote)){
        echo( fread($remote, 4096));
    }
    

    I have not tested the code, but it should work.

提交回复
热议问题