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

余生长醉 提交于 2019-12-01 09:13:30

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.

You can use phpseclib to download the file:

require_once 'Net/SFTP.php';

$connection = new Net_SFTP($remote_server_ip);
if (!$connection->login('username', 'password')) die('Login Error');

// set some appropriate content headers
echo $connection->get($filelink);

Or you can use the ssh2.sftp wrapper - see SilvioQ's answer for that approach.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!