PHP调用SFTP封装

PHP调用SFTP封装

<?php
namespace App\Utils;

class Sftp
{
    private $conn;
    private $sftp;

    public function __construct($host, $port, $username, $password, $localDir = '.', $remoteDir = '/')
    {
        $this->conn = \ssh2_connect($host, $port);
        \ssh2_auth_password($this->conn, $username, $password);
        $this->sftp = \ssh2_sftp($this->conn);
        $this->localDir = rtrim($localDir, '/');
        $this->remoteDir = rtrim($remoteDir, '/');
    }


    public function download($remoteFile, $localFile)
    {
        if (!copy("ssh2.sftp://{$this->sftp}{$remoteFile}", $localFile)) {
            throw new \Exception('Unable to download file.');
        }
    }


    public function upload($localFile, $remoteFile, $overwrite = true)
    {
        if ($overwrite || !$this->fileExists($remoteFile)) {
            if (!copy($localFile, "ssh2.sftp://{$this->sftp}{$remoteFile}")) {
                throw new \Exception('Unable to upload file.');
            }
        } else {
            throw new \Exception('File already exists on remote server.');
        }
    }


    public function delete($remoteFile)
    {
        if (!$this->fileExists($remoteFile)) {
            throw new \Exception('File does not exist on remote server.');
        }

        if (!unlink("ssh2.sftp://{$this->sftp}{$remoteFile}")) {
            throw new \Exception('Unable to delete file.');
        }
    }


    public function copy($srcFile, $destFile, $overwrite = true)
    {
        if (!$this->fileExists($srcFile)) {
            throw new \Exception('Source file does not exist on remote server.');
        }

        if ($overwrite || !$this->fileExists($destFile)) {
            if (!copy("ssh2.sftp://{$this->sftp}{$srcFile}", "ssh2.sftp://{$this->sftp}{$destFile}")) {
                throw new \Exception('Unable to copy file.');
            }
        } else {
            throw new \Exception('Destination file already exists on remote server.');
        }
    }


    public function move($srcFile, $destFile, $overwrite = true)
    {
        if (!$this->fileExists($srcFile)) {
            throw new \Exception('Source file does not exist on remote server.');
        }

        if ($overwrite || !$this->fileExists($destFile)) {
            if (!rename("ssh2.sftp://{$this->sftp}{$srcFile}", "ssh2.sftp://{$this->sftp}{$destFile}")) {
                throw new \Exception('Unable to move file.');
            }
        } else {
            throw new \Exception('Destination file already exists on remote server.');
        }
    }


    public function backup($srcFile, $backupFile, $suffix = '_backup')
    {
        if (!$this->fileExists($srcFile)) {
            throw new \Exception('File does not exist on remote server.');
        }

        if ($this->fileExists($backupFile)) {
            throw new \Exception('Backup file already exists on remote server.');
        }

        if (!copy("ssh2.sftp://{$this->sftp}{$srcFile}", "ssh2.sftp://{$this->sftp}{$backupFile}")) {
            throw new \Exception('Unable to create backup file.');
        }
    }


    public function listDirectory($dir) {
        $handle = opendir("ssh2.sftp://{$this->sftp}{$dir}");
        $files = array();
        while (($file = readdir($handle)) !== false) {
            if ($file != '.' && $file != '..') {
                $files[] = $file;
            }
        }
        closedir($handle);
        return $files;
    }


    public function fileExists($remoteFile)
    {
        return file_exists("ssh2.sftp://{$this->sftp}{$remoteFile}");
    }


    public function __destruct()
    {
        \ssh2_disconnect($this->conn);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大叔是90后大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值