简体   繁体   English

如何将文件从Sftp服务器下载到本地计算机

[英]How to download file from Sftp server to local machine

I have to download file from Sftp server to local machine. 我必须将文件从Sftp服务器下载到本地计算机。 I am creating a connection by using the phpseclib. 我正在使用phpseclib创建连接。 It is connected with Sftp server. 它与Sftp服务器连接。 But I have to download a file from server to local machine. 但是我必须将文件从服务器下载到本地计算机。 How can i do that. 我怎样才能做到这一点。 I am using this code to download dummy.txt from remote server to local machine. 我正在使用此代码从远程服务器将dummy.txt下载到本地计算机。 Please help 请帮忙

public function startProcess() {

    $sftp = new Net_SFTP('www.domain.com');

    if (!$sftp->login('username', 'password')) {
        exit('Login Failed');
    }
    else {
        echo "connected";
    }

    echo $sftp->pwd() . "\r\n";
    downloadfiles('/path to server/dummy.txt');      
}

public function downloadfiles($filename)
{
    if($filename)
    {
        $remote=file_get_contents($filename);
        $local_file_path='localpath/adobe.txt';
        file_put_contents($local_file_path,$remote);
    }
    else
    {
        echo "error download files";
    }

}

Please help me to sought it out. 请帮助我寻找它。

Well you're not doing $sftp->get() anywhere, for one. 好吧,您不是在任何地方都执行$sftp->get()

Below is a re-worked version of your code to enable downloads: 以下是经过重新设计的代码版本,可以进行下载:

global $sftp;

public function startProcess() {

    $sftp = new Net_SFTP('www.domain.com');

    if (!$sftp->login('username', 'password')) {
        exit('Login Failed');
    }
    else {
        echo "connected";
    }

    echo $sftp->pwd() . "\r\n";
    downloadfiles('/path to server/dummy.txt');      
}

public function downloadfiles($filename)
{
    if($filename)
    {
        $local_file_path='localpath/adobe.txt';
        $sftp->get($filename, $local_file_path);
    }
    else
    {
        echo "error download files";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM