Using the SFTP Tutorial in PHP _php tutorial

Source: Internet
Author: User
Tags ftp connection file transfer protocol

Tutorial on using SFTP in PHP


 config = $config; }//FTP connection Public function connect () {return $this->conn = ftp_connect ($this->config[' host '), $this->config[' Port '])); }//Transport Data Transfer layer protocol, get data true or false public function download ($remote, $local, $mode = ' auto ') {return $result = @ftp_ge   T ($this->conn, $localpath, $remotepath, $mode); }//Transmit Data Transfer layer protocol, upload true or false public function upload ($remote, $local, $mode = ' auto ') {return $result = @ft   P_put ($this->conn, $localpath, $remotepath, $mode);   }//Delete file Public function remove ($remote) {return $result = @ftp_delete ($this->conn_id, $file);} }//uses $config = array (' hostname ' = ' localhost ', ' username ' = ' root ', ' password ' + ' root ' ', ' port ' = 21); $FTP = new FTP (), $ftp->connect ($config), $ftp->upload (' Ftp_err.log ', ' ftp_upload.log '); $ftp->download (' FTP _upload.log ', ' ftp_download.log ');/* Write an SSH-based FTP class based on the three protocols above we know there are two ways to authenticate: Public key, password;(1) login with password (2) password-free login is to use the public key login * * Classsftp{//Initial configuration is nullprivate $config =null;//connection is NULL private $conn = null;//use secret key to log in private $use _pubkey_file= false;// Initialize Public function init ($config) {$this->config = $config;}  Connect to SSH, there are two ways to connect (1) Use password//(2) Use secret key public function connect () {$methods [' hostkey '] = $use _pubkey_file? ' Ssh-rsa ': []; $con = Ssh2_connect ($this->config[' host '), $this->config[' Port '], $methods);//(1) If the secret key is used ($use _pubkey_file) {//user authentication protocol $RC = Ssh2_auth_pubkey_file ($conn, $this->config[' user '], $this->config[' Pubkey_file '), $this- config[' Privkey_file '), $this->config[' passphrase '));//(2) Use login user name and login password}else{$RC = Ssh2_auth_password ($conn, $            this->conf_[' user '], $this->conf_[' passwd ');  }return $RC; The}//transmits the data Transfer layer protocol, obtains the public function download ($remote, $local) {return ssh2_scp_recv ($this->conn_, $remote, $lo   CAL); }//Transmit Data Transfer layer protocol, write FTP server data public function upload ($remote, $local, $file _mode=0664) {return ssh2_scp_send ($this Conn_, $local, $Remote, $file _mode);        }//Delete file Public function remove ($remote) {$sftp = Ssh2_sftp ($this->conn_); $RC = false; if (Is_dir (ssh2.sftp://{$sftp}/{$remote})) {$RC = false;//SSH Delete folder $RC = Ssh2_sftp_rmdir ($sftp, $remote);} E LSE {//delete file $RC = Ssh2_sftp_unlink ($sftp, $remote);}     return $RC;}    } $config = [host = = 192.168.1.1,//FTP Address user = * * *, port = 22, Pubkey_path =/root/.ssh/id_rsa.pub,//The storage address of the public key Privkey_path =/root/.ssh/id_rsa,//The storage address of the private key]; $h Andle = new Sftpaccess (), $handle->init ($config), $RC = $handle->connect (), $handle->getdata (remote, $local);  *

http://www.bkjia.com/PHPjc/962645.html www.bkjia.com true http://www.bkjia.com/PHPjc/962645.html techarticle sftp in php using tutorial config = $config;} FTP Connect public Function connect () {return $this-conn = Ftp_connect ($this-config[host], $this-config[port])); }//transfer data transmission ...

  • Related Article

    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.