PHP SFTP Implementation of upload and download functions of the example of detailed

Source: Internet
Author: User
Tags file transfer protocol ftp transfer
This article will first introduce you to the SFTP, after we mainly share with you the SFTP upload download function instance, I hope to help everyone.

First, the introduction of SFTP:

The protocol for FTP transfer using the SSH protocol is called SFTP (secure file transfer) both SFTP and FTP are file transfer protocols. Difference: SFTP is an SSH protocol (SSH is an encrypted Telnet protocol), as long as the sshd server is started, it is available, and SFTP security is high, it does not require the FTP server to boot. SFTP = SSH + FTP (secure file Transfer Protocol). Because FTP is transmitted in plaintext, there is no security, and SFTP is based on SSH, the transmission content is encrypted and more secure. At present the network is not very safe, used telnet to use SSH2 (SSH1 has been cracked).

SFTP This tool is the same as FTP usage. However, its transfer files are encrypted by SSL, even if intercepted and can not be cracked. and sftp more than the FTP function, some of the file property settings.

Second, SSH2 extended configuration

1. Download Address: http://windows.php.net/downloads/pecl/releases/ssh2/0.12/

Choose the extension package according to your PHP version, here I use php5.3, so I downloaded the php_ssh2-0.12-5.3-ts-vc9-x86.zip (download link)

2. After the decompression, there will be three files, Libssh2.dll, Php_ssh.dll, php_ssh2.pdb.

3. Put Php_ssh.dll, php_ssh2.pdb in your PHP extension directory under php/ext/.

4. Copy the Libssh2.dll to the C:/windows/system32 and C:/WINDOWS/SYSWOW64

5. Add Extension=php_ssh2.dll to PHP.ini

6. Restart Apache, print phpinfo (); The SSH2 extension appears, indicating that the installation was successful

Three, the SFTP code demo

Calling code


$config = Array (  ' host ' = ' 211.*.*.* ',//server  ' port ' = ' 23 ',//Port  ' username ' + ' test ',//user name  ') Password ' = * * * * * * *,//password); $ftp = new Sftp ($config); $localpath = "E:/www/new_20170724.csv"; $serverpath = '/new_20170724.csv '; $st = $ftp->upftp ($localpath, $serverpath); Upload the specified file if ($st = = True) {  echo "success";   } else{  echo "fail";}

SFTP Package Class


<?php/** * Sftp upload download file * */namespace Common\org\util;class Sftp{//initial configuration is null private $config = NULL; The connection is null private $conn = 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 ']) {}else{echo ') Unable to authenticate on the server "; }}//Transmit Data Transfer layer protocol, get the public function downftp ($remote, $local) {$ressftp = Ssh2_sftp ($this->conn); return copy ("SS h2.sftp://{$ressftp} ". $remote, $local); }//Transport data Transfer layer protocol, write FTP server data public function upftp ($local, $remote, $file _mode = 0777) {$ressftp = Ssh2_sftp ($this->co NN);   return copy ($local, "ssh2.sftp://{$ressftp}". $remote); } }
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.