使用sftp需要安装扩展!windows & centos7.6下安装ssh2的php7.3扩展(不同对应版本)
sftp类
class Sftp {
// 初始配置为NULL
private $config = NULL;
// 连接为NULL
private $conn = NULL;
//sftp resource
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{
return "用户名或密码错误";
}
}
// 下载文件
public function downftp($remote, $local)
{
return copy("ssh2.sftp://{$this->ressftp}".$remote, $local);
}
// 文件上传
public function upftp( $local,$remote, $file_mode = 0777)
{
return copy($local,"ssh2.sftp://{$this->ressftp}".$remote);
}
//创建目录
public function ssh2_sftp_mchkdir($path) //使用创建目录循环
{
ssh2_sftp_mkdir($this->ressftp, $path,0777,true);
}
//判段目录是否存在
public function ssh2_dir_exits($dir){
return file_exists("ssh2.sftp://{$this->ressftp}".$dir);
}
}
上传
public function SftpUpdate(){
//本地文件目录
$localpath = "/test.txt"; //本地文件路径
$serverpath='/data/test'; //远程目录(需要上传到的目录)
$config = array("host"=>"IP","username"=>"账号","port"=>"22","password"=>"密码");
try {
$sftp = new \App\Model\Sftp($config);
$res = $sftp->ssh2_dir_exits("$serverpath");
//如果目录存在直接上传
if($res){
$sftp->upftp($localpath,$serverpath);
}else{
$sftp->ssh2_sftp_mchkdir($serverpath);
$sftp->upftp($localpath,$serverpath);
}
return 'ok';
} catch (\Exception $e) {
return $e->getMessage();
}
}
Ps:本地和远程路径一定要准确!!!