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 1306
野趣味
野趣味 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.

    0 讨论(0)
  • 2021-01-14 05:02

    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.

    0 讨论(0)
提交回复
热议问题