一个用php写的sftp上传下载文件类
<?php
/**
* SFtp上传下载文件
*
*/
namespace app\utils\sftp;
class Sftp
{
// 初始配置为NULL
private $config = NULL;
// 连接为NULL
private $conn = NULL;
private $ressftp = NULL;
// 初始化
public function __construct($config)
{
$this->config = $config;
$this->connect();
}
public function connect()
{
$this->conn = ssh2_connect($this->config['host'], $this->config['port']);
if( ssh2_auth_password($this->conn, $this->config['username'], $this->config['password']))
{
$this->ressftp = ssh2_sftp($this->conn);
}else{
echo "unconnect";
}
}
// 传输数据 传输层协议,获得数据
public function downftp($remote, $local)
{
return copy("ssh2.sftp://{$this->ressftp}".$remote, $local);
}
// 传输数据 传输层协议,写入ftp服务器数据
public function upftp( $local,$remote, $file_mode = 0777)
{
return copy($local,"ssh2.sftp://{$this->ressftp}".$remote);
}
//创建目录
public function ssh2_sftp_mchkdir($path, $file_mode = 0777) //使用创建目录循环
{
ssh2_sftp_mkdir($this->ressftp, $path, $file_mode);
}
//判段目录是否存在
public function ssh2_dir_exits($dir)
{
return file_exists("ssh2.sftp://{$this->ressftp}" . $dir);
}
}