美文网首页程序员的成长之道
php 利用phpseclib扩展包,实现SFTP管理文件

php 利用phpseclib扩展包,实现SFTP管理文件

作者: 菜菜菜鸟羊 | 来源:发表于2019-09-27 17:01 被阅读0次

-github:(https://github.com/phpseclib/phpseclib)

<?php

//require_once 'abstract.php';
require_once "shell/vendor/autoload.php";
use phpseclib\Net\SFTP;

/**
 * Class HP_Shell_Stock
 */
class HP_Shell_File
{
    const REMOTE_DIR = '/home/server/Public/';
    private $host = '192.168.8.113';
    private $server = 'server';
    private $password = ' ';

    public function getLocalDir()
    {
        return Mage::getBaseDir('var') . '/content_xml_downloading';
    }

    public function getLocalDestDir()
    {
        return Mage::getBaseDir('media') . '/contents_xml';
    }

    public function run(){
        $nameArr = array();
        $needFil = array();
        $sftp = new SFTP($this->host);
        if (!$sftp->login($this->server,$this->password)) {
            throw new Exception("Login failed");
        }else{
            echo "<h1>it works</h1><br>";
            //download
            $fileName = $sftp->nlist(self::REMOTE_DIR);
            foreach ($fileName as $name) {
                if (strstr($name , 'cap_cn_zh_xml_')) {
                    echo $name.'<br>';
                    array_push($needFil,$name);
                    $sftp->get(self::REMOTE_DIR.$name,$this->getLocalDir()."/$name");
                    preg_match_all("/cap_cn_zh_xml_[\d]{4}_([\d]{6})/",$name,$out,PREG_SET_ORDER);
                    array_push($nameArr,$out[0][1]);
                }
            }
            $this->extractFile($needFil);
            $this->createFile($nameArr,$fileName);
            $this->moveFiles($nameArr,$needFil,$fileName);
        }
    }

    /**
     * 移动文件到指定文件
     */
    public function moveFiles($nameArr,$needFil,$fileName){
        $sftp = new SFTP($this->host);
        if (!$sftp->login($this->server,$this->password)) {
            throw new Exception("Login failed");
        }else{
            $remoteFil = array();
            foreach ($fileName as $value){
                preg_match_all("/^\d{6}/",$value,$out,PREG_SET_ORDER);
                if (!empty($out)){
                    array_push($remoteFil,$out[0][0]);
                }
            }
            $sftp->chdir(self::REMOTE_DIR);//改变目录
            foreach ($needFil as $valueNd){
                foreach (array_unique($nameArr) as $key=>$valueNa){
                    if (in_array($valueNa,$remoteFil)){
                        if (strstr($valueNd,$valueNa)){
                            $sftp->rename($valueNd,$valueNa.'/'.$valueNd);
                        }
                    }else{
                        $sftp->mkdir($valueNa);
                        $sftp->rename($valueNd,$valueNa.'/'.$valueNd);
                    }
                }
            }
        }
    }

/**
     * 解压文件
     */
    public function extractFile($name) {
        echo $_SERVER['PATH_INFO'];
        if (count($name) != 0){
            foreach ($name as $value){
                $zip=new ZipArchive();
                chmod("/usr/share/nginx/html/getremotefiles/$value",0777);
                if($zip->open("/usr/share/nginx/html/getremotefiles/$value")===TRUE){
                    $zip->extractTo("/usr/share/nginx/html/getremotefiles/abc");
                    file_put_contents("/usr/share/nginx/html/hp.php9.cc/var/log/success.txt",$value."\n",FILE_APPEND);
                    $zip->close();
                }else{
                    file_put_contents("/usr/share/nginx/html/hp.php9.cc/var/log/failed.txt",$value."\n",FILE_APPEND);
                }
            }
        }
    }


    /**
     * 根据文件创建文件夹
     */
//    public function createFile($arr,$fiName) {
//        $sftp = new SFTP("192.168.8.113");
//        if (!$sftp->login("server"," ")){
//            throw new Exception("Login failed");
//        }else{
//            foreach (array_unique($arr) as $key=>$one) {
//                foreach ($fiName as $two){
//                    if ($one == $two){
//                        unset($arr[$key]);
//                    }
//                }
//            }
//            $sftp->chdir("/home/server/Public/");//改变目录
//            foreach (array_unique($arr) as $value) {        //创建目录
//                $sftp->mkdir($value);
//            }
//        }
//        return;
//    }
}

$shell = new HP_Shell_File();
$shell->run();

-主要使用SFTP

相关文章

网友评论

    本文标题:php 利用phpseclib扩展包,实现SFTP管理文件

    本文链接:https://www.haomeiwen.com/subject/hodauctx.html