<?phpnamespaceApp\Utils;classSftp{private$conn;private$sftp;publicfunction__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,'/');}publicfunctiondownload($remoteFile,$localFile){if(!copy("ssh2.sftp://{$this->sftp}{$remoteFile}",$localFile)){thrownew\Exception('Unable to download file.');}}publicfunctionupload($localFile,$remoteFile,$overwrite=true){if($overwrite||!$this->fileExists($remoteFile)){if(!copy($localFile,"ssh2.sftp://{$this->sftp}{$remoteFile}")){thrownew\Exception('Unable to upload file.');}}else{thrownew\Exception('File already exists on remote server.');}}publicfunctiondelete($remoteFile){if(!$this->fileExists($remoteFile)){thrownew\Exception('File does not exist on remote server.');}if(!unlink("ssh2.sftp://{$this->sftp}{$remoteFile}")){thrownew\Exception('Unable to delete file.');}}publicfunctioncopy($srcFile,$destFile,$overwrite=true){if(!$this->fileExists($srcFile)){thrownew\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}")){thrownew\Exception('Unable to copy file.');}}else{thrownew\Exception('Destination file already exists on remote server.');}}publicfunctionmove($srcFile,$destFile,$overwrite=true){if(!$this->fileExists($srcFile)){thrownew\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}")){thrownew\Exception('Unable to move file.');}}else{thrownew\Exception('Destination file already exists on remote server.');}}publicfunctionbackup($srcFile,$backupFile,$suffix='_backup'){if(!$this->fileExists($srcFile)){thrownew\Exception('File does not exist on remote server.');}if($this->fileExists($backupFile)){thrownew\Exception('Backup file already exists on remote server.');}if(!copy("ssh2.sftp://{$this->sftp}{$srcFile}","ssh2.sftp://{$this->sftp}{$backupFile}")){thrownew\Exception('Unable to create backup file.');}}publicfunctionlistDirectory($dir){$handle=opendir("ssh2.sftp://{$this->sftp}{$dir}");$files=array();while(($file=readdir($handle))!==false){if($file!='.'&&$file!='..'){$files[]=$file;}}closedir($handle);return$files;}publicfunctionfileExists($remoteFile){returnfile_exists("ssh2.sftp://{$this->sftp}{$remoteFile}");}publicfunction__destruct(){\ssh2_disconnect($this->conn);}}