Rupzz
Rupzz

Reputation: 146

How to download file from Sftp server to local machine

I have to download file from Sftp server to local machine. I am creating a connection by using the phpseclib. It is connected with Sftp server. 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. 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.

Upvotes: 1

Views: 4426

Answers (1)

neubert
neubert

Reputation: 16792

Well you're not doing $sftp->get() anywhere, for one.

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";
    }

}

Upvotes: 2

Related Questions