php实现sftp上传下载

 [
        'host'     => 'data.sftp.daiyuanpei.com',
        'port'     => '3333',
        'user'     => 'data-online',
        'password' => 'SFTPDAIYUANPEI',
        'root'     => 'upload/online'
    ]
];
config = require(ROOT . "/conf/system.conf.php");
        $this->config = $this->config['sftp'];
        $this->conn = ssh2_connect($this->config['host'], $this->config['port']);
        ssh2_auth_password($this->conn, $this->config['user'], $this->config['password']);
        return $this;
    }

    public function download($remote, $local)
    {
        $ressftp = ssh2_sftp($this->conn);
        return copy("ssh2.sftp://{$ressftp}/$remote", $local);
    }

    public function upload($local, $remote)
    {
        $ressftp = ssh2_sftp($this->conn);
        return copy($local, "ssh2.sftp://{$ressftp}/$remote");
    }

    public function getRoot($path)
    {
        return $this->config['root'] . '/' . $path;
    }
}
connect();
$ftpPath = $ftp_dir.'/'. "$current.dat";
$md5FtpPath = $ftp_dir.'/' . "$current.md5";
echo $ftpPath.PHP_EOL;
echo $md5FtpPath.PHP_EOL;
// $md5 = md5_file($ftpPath);
// echo $md5;
// file_put_contents($md5FtpPath, $md5);
// die;

$ftpPath = $ftp->getRoot($ftpPath);
$md5FtpPath = $ftp->getRoot($md5FtpPath);
echo "upload $md5File and $dataFile to sftp.".PHP_EOL;
$dataFile ='/home/work/scripts/gp-data/'.$ftp_dir.'/'.$current.'.dat';
$md5File ='/home/work/scripts/gp-data/'.$ftp_dir.'/'.$current.'.md5';
echo "source_data:".$dataFile.PHP_EOL;
echo "ftp dir".$ftpPath.PHP_EOL;
echo "md5_source_data:".$md5File.PHP_EOL;
echo "md5 ftp dir".$md5FtpPath.PHP_EOL;
//die;
$a = $ftp->upload($dataFile, $ftpPath);
$b = $ftp->upload($md5File, $md5FtpPath);
var_dump($a, $b);

你可能感兴趣的:(PHP,sftp,上传文件,下载文件)