Php-sftp File Upload

Source: Internet
Author: User
Tags php class

Personal blog Address https://xgs888.top/post/view?id=57

PHP's sftp file upload; used in frame thinkphp5;

The main use of the SFTP function is as follows;

Ssh2_connect (host,port) Link information

Ssh2_auth_password ($con, Username,password) login Verification

Copy ();//File replication between servers;

Requirements are as follows

The required file field information can be found in the attachment, note the last column with the date of the month and minute format;

Before uploading, you need to determine whether there is a date folder in the corresponding directory, if none, then create, if any, directly upload to the appropriate folder, as shown in the attachment

A non-composer installed third-party class library directory is available in THINKPHP5 extend

Create an SFTP directory under the Extend directory to create a sftp.php class

namespace sftp;class sftp{   //  Initial configuration for null private  $config  =  null; //  connection for null private  $conn  = NULL; //sftp resource   private  $ressftp  = NULL; //  Initialize  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  "username or password error"; }    }   //  download File  public function downftp ($remote,  $local)  {   return copy ("ssh2.sftp://{$ressftp} ". $remote,  $local); }   //  file upload  public function  upftp (  $local, $remote,  $file _mode = 0777)  {  return copy ($local, "Ssh2.sftp://{$this->ressftp}". $remote);     } //Create directory  public function  ssh2_sftp_mchkdir ($path)   //use Create directory loop {ssh2_sftp_mkdir ($this->ressftp,  $path, 0777);} Public function ssh2_dir_exits ($dir) {return file_exists ("ssh2.sftp://{$this RESSFTP} ". $dir);}   }


The following is called inside the controller

namespace app\index\controller;use think\controller;use \sftp\sftp;class index  Extends controller{    public function index ()     {     ini_set (' max_execution_time ', ' 0 ');//Set never timeout      $time  =  Date (' Ymd ', Time () -24*3600);//Last day      $name  =  "Mac". $time;           $data  = db (' table ')->where ("Add_date= ' 2017-10-09 '") Select ();//query Data           $fp  = fopen ("$name. csv", "a");  //Open the CSV file and create   $datastr  =  ' id,sitecode,devmac,stamac,add_date,up_time ' if it does not exist. \ r \ n "; foreach  ($data  as  $key  =>  $value)  { # code... $ datastr =  $datastr. $value [' id ']. $value [' sitecode ']. ', '. $value [' Devmac '].  ', '. $value [' Stamac ']. ' , '. $value [' add_date ']. ', '. $value [' Up_time ']. " \r\n ";  } //iconv (' GB2312 ', ' Utf-8//ignore ', $datastr)  fwrite ($fp, $datastr);  //Write data gbk turn UTF8  fclose ($FP);  //close file handle  echo  "Build succeeded"; $config  = array (' host ' = ' 127.0.0.1 ', ' Username ' + ' root ', ' password ' + ' root ', ' port ' = ' + ', $sftp  = new sftp ($config); $re   =  $sftp->ssh2_dir_exits ("/test/$time"), if ($re) {//If the directory has a direct upload $sftp->upftp ("$name. csv", '/test/'. $time. ' /'. $name. '. CSV ');} else{$sftp->ssh2_sftp_mchkdir ('/test/' $time) $sftp->upftp ("$name. csv", '/test/'. $time. ' /'. $name. '. CSV ');}     }  }


Php-sftp File Upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.