Laravel 实现 SFTP 读取数据库数据上传 CSV 文件

控制器:

 public function getSftpData(){
    ini_set('max_execution_time','0');//设置永不超时
    $time = date('Ymd',time());
    $name = "openid".$time;

    $data = 'id,标题,作者,内容,状态,浏览量,时创建间'."\r\n";
    $list = Test::orderBy('id','desc')->get(['id','title','author','images','description','status','view','created_at'])->toArray();
    $fp = fopen("$name.csv","a"); //打开csv文件,如果不存在则创建
    foreach ($list as $key => $value) {
        $data = $data.$value['id'].','.$value['title'].','.$value['author'].','.$value['images'].','.$value['description'].','.$value['status'].','.$value['view'].','.$value['created_at']."\r\n";
    }
    fwrite($fp,$data); //写入数据讲gbk转成utf8
    fclose($fp); //关闭文件句柄

    $config = array("host"=>"ip","username"=>"user","port"=>"22","password"=>"password");

    try {
        $sftp = new \sftpData($config);

        $re = $sftp->ssh2_dir_exits("/upload/$time");
        //如果目录存在直接上传
        if($re){
            $sftp->upftp("$name.csv",'/upload/'.$time.'/'.$name.'.csv');
        }else{
            $sftp->ssh2_sftp_mchkdir("/upload/$time");
            $sftp->upftp("$name.csv",'/upload/'.$time.'/'.$name.'.csv');
        }

        die('ok');
    } catch (\Exception $e) {
        die('连接失败!');
    }
}

sftp类:

<?php
/******************************************** 
* MODULE:SFTP类 
*******************************************/
class sftpData{
        // 初始配置为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{ 
                echo "用户名或密码错误";
         }

        }

        // 下载文件
        public function downftp($remote, $local)
        { 
                return copy("ssh2.sftp://{$ressftp}".$remote, $local);
        }

        // 文件上传
        public function upftp( $local,$remote, $file_mode = 0777)
        { 
                return copy($local,"ssh2.sftp://{$this->ressftp}".$remote); 
                // return ssh2_scp_send($this->ressftp,$local,$remote,$file_mode); 
        }
        //创建目录
        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);
        }
}
?>
本作品采用《CC 协议》,转载必须注明作者和本文链接
不要轻易放弃。学习成长的路上,我们长路漫漫,只因学无止境 Don't give up easily. On the way of learning and growing up, we have a long way to go, just because there is no end to learning.
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!