1.自己写写的一个ssh连接操作文件及文件夹,记录一下,有错误或不足的地方欢迎大家提出,方便进行修改,谢谢!
/**
* Class File
* file 文件管理
*/
class File extends MY_Controller {
private $connection;
private $sftp;
public function __construct()
{
parent::__construct();
@ob_clean() ;
@session_start() ;
$this->sft_login();
}
/**
*功能:文件列表
*User:qiangzi Date:2020-05-12 Time:10:28
*/
function ajax_list(){
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
exit(result_to_towf('', 400, '请求失败,请登录后再试。'));
}
//$mr_path='/home/'.$_SESSION['username'];
$path = '/';
$type = 'nlist';
if(isset($_GET['path']) || !empty($_GET['path'])){
$path = $_GET['path'];
}
if(isset($_GET['type']) || !empty($_GET['type'])){
$type = $_GET['type'];
}
$am_ftp_rawlist_array = array();
$sftp1 = $this->sftp;
if($type=='mkdir'){
if(!isset($_GET['dir_name']) || empty($_GET['dir_name'])){
exit(result_to_towf('', 400, '文件夹名称不能为空'));
}
$mkdir_path = '/home/'.$_SESSION['username'].$path.'/'.$_GET['dir_name'];
$res = $this->mkdir($sftp1,$mkdir_path);
if($res){
exit(result_to_towf('', 200, '创建成功'));
}else{
exit(result_to_towf('', 400, '创建失败'));
}
}elseif ($type=='attribute'){
$del_path = '/home/'.$_SESSION['username'].$path;
$res = $this->attribute($sftp1,$del_path);
if($res){
exit(result_to_towf($res, 200, '获取成功'));
}else{
exit(result_to_towf('', 400, '获取失败'));
}
}elseif ($type=='show'){
$res = $this->get_contents($sftp1,$path);
if($res){
if(!empty($res)){
$res = str_replace( "\n", "
", $res);
}
exit(result_to_towf($res, 200, '获取成功'));
}else{
exit(result_to_towf('', 400, '暂无数据'));
}
}elseif ($type=='del'){
$del_path = '/home/'.$_SESSION['username'].$path;
$res = $this->del_file($sftp1,$del_path);
if($res){
exit(result_to_towf('', 200, '删除成功'));
}else{
exit(result_to_towf('', 400, '删除失败'));
}
} else{
// if($path=='/'){
// $path='/';
// }
//$am_ftp_rawlist = scandir($mr_path.$path);//读取目录
$handle = opendir('ssh2.sftp://'.intval($sftp1).'/home/'.$_SESSION['username'].$path);
$am_ftp_rawlist=array();
while(false != ($entry = readdir($handle))){
$am_ftp_rawlist[] = $entry;
}
closedir($handle);
$superior_path='/';
if($path!='/'){
$superior_path = substr($path,0,strrpos($path,'/'));
}
$am_ftp_rawlist_array[0]['dirfilename']=$path; //文件夹名称
$am_ftp_rawlist_array[0]['file_type']='dir';
$am_ftp_rawlist_array[0]['can_open']='y';
$am_ftp_rawlist_array[0]['path']=$superior_path;
$am_ftp_rawlist_array[0]['name']='..';
if(!empty($am_ftp_rawlist)){
foreach ($am_ftp_rawlist as $key => $val) {
$row = functions::Grawlistline($val);
if(!in_array($row['dirfilename'], array('.', '..')) && substr($row['dirfilename'],0,1)!='.' && strpos($row['dirfilename'],'/.')===false)
{
$am_ftp_rawlist1 = strstr($row['dirfilename'],'.');
if($am_ftp_rawlist1===false){
$row['file_type']='dir';
}else{
$row['file_type']='file';
}
$row['can_open']=($row['file_type'] == 'dir') ? 'y':'n';
if(substr($path,-1)!='/'){
$row['path']=$path.'/'.$row['dirfilename'];
}else{
$row['path']=$path.$row['dirfilename'];
}
$row['name']=$row['dirfilename'];
$am_ftp_rawlist_array[] = $row;
}
}
}
exit(result_to_towf($am_ftp_rawlist_array, 200, '请求成功'));
}
exit(result_to_towf('', 400, '请求失败'));
}
/**
*功能:sftp连接
* @throws Exception
*User:qiangzi
*Date:2020/7/2 Time:11:55
*/
public function sft_login(){
if(!isset($_SESSION['uid']) || empty($_SESSION['uid'])){
return false;
}
$info = $this->M_user->getOneUser($_SESSION['uid'],'user_name,ssh_pass');
if(empty($info)){
return false;
}
$ssh_pass = AES_ADMIN::decrypt($info['ssh_pass']);//ssh密码解密
$this->connection = ssh2_connect(SSH_HOST, SSH_PORT);
if(ssh2_auth_password($this->connection, $info['user_name'], $ssh_pass)) {
$this->sftp = ssh2_sftp($this->connection);
return true;
}else{
return false;
}
}
/**
*功能:写文件
*User:qiangzi
*Date:2020/7/3 Time:16:39
*/
public function edit_file(){
$sftp1 = $this->sftp;
$counts = $_POST['counts'];
$mr_path='/home/'.$_SESSION['username'];
$path = $_POST['path'];
$stream = fopen("ssh2.sftp://".intval($sftp1).$mr_path.$path, 'w+');
fwrite($stream, $counts);
fclose($stream);
}
/**
*功能:删除文件
* @param $sftp1
* @param $path
* @return bool
* *User:qiangzi
*Date:2020/7/3 Time:16:04
*/
public function del_file($sftp1,$path){
return ssh2_sftp_unlink($sftp1, $path);
}
/**
*功能:创建目录
* @param $sftp1
* @param $path
* @return bool
* *User:qiangzi
*Date:2020/7/3 Time:16:04
*/
public function mkdir($sftp1,$path){
return ssh2_sftp_mkdir($sftp1, $path);//创建目录
}
/**
*功能:获取文件属性
* @param $sftp1
* @param $path
* @return array
* User:qiangzi
* Date:2020/7/3 Time:16:04
*/
public function attribute($sftp1,$path){
$data = array();
$data['type'] = filetype("ssh2.sftp://".intval($sftp1).$path);
$data['size'] = filesize("ssh2.sftp://".intval($sftp1).$path).' bytes';
$data['up_time'] = date("Y-m-d H:i:s",filemtime("ssh2.sftp://".intval($sftp1).$path));
return $data;
}
/**
*功能:获取文件内容
* @param $sftp1
* @param $path
* @return false|string
* User:qiangzi
*Date:2020/7/3 Time:16:04
*/
public function get_contents($sftp1,$path){
return file_get_contents("ssh2.sftp://".intval($sftp1).$path);
}
}