《PHP應(yīng)用:PHP SFTP實(shí)現(xiàn)上傳下載功能》要點(diǎn):
本文介紹了PHP應(yīng)用:PHP SFTP實(shí)現(xiàn)上傳下載功能,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用?一、SFTP介紹:
PHP應(yīng)用使用SSH協(xié)議進(jìn)行FTP傳輸?shù)膮f(xié)議叫SFTP(安全文件傳輸)Sftp和Ftp都是文件傳輸協(xié)議.區(qū)別:sftp是ssh內(nèi)含的協(xié)議(ssh是加密的telnet協(xié)議),?只要sshd服務(wù)器啟動(dòng)了,它就可用,而且sftp安全性較高,它本身不需要ftp服務(wù)器啟動(dòng). sftp = ssh + ftp(安全文件傳輸協(xié)議).由于ftp是明文傳輸?shù)??沒有安全性,而sftp基于ssh,傳輸內(nèi)容是加密過的,較為安全.目前網(wǎng)絡(luò)不太安全,以前用telnet的都改用ssh2(SSH1已被破解).
PHP應(yīng)用sftp這個(gè)工具和ftp用法一樣.但是它的傳輸文件是通過ssl加密了的,即使被截獲了也無法破解.而且sftp相比ftp功能要多一些,多了一些文件屬性的設(shè)置.?
PHP應(yīng)用二、SSH2擴(kuò)展配置
PHP應(yīng)用1.??下載地址:http://windows.php.net/downloads/pecl/releases/ssh2/0.12/
PHP應(yīng)用根據(jù)自己的php版本選擇 擴(kuò)展包,這里我使用的是php5.3,所以我下載的是 php_ssh2-0.12-5.3-ts-vc9-x86.zip(下載鏈接)
PHP應(yīng)用
PHP應(yīng)用2. 解壓完后,會(huì)有三個(gè)文件,libssh2.dll、php_ssh.dll、php_ssh2.pdb.?
PHP應(yīng)用3. 將 php_ssh.dll、php_ssh2.pdb 放到你的 php 擴(kuò)展目錄下 php/ext/ 下.?
PHP應(yīng)用4. 將libssh2.dll 復(fù)制到 c:/windows/system32 和 c:/windows/syswow64 各一份?
PHP應(yīng)用5.在 php.ini中加入 extension=php_ssh2.dll
PHP應(yīng)用6.重啟Apache, 打印phpinfo(); 會(huì)出現(xiàn) SSH2 擴(kuò)展,表示安裝成功
PHP應(yīng)用
PHP應(yīng)用三、SFTP 代碼DEMO
PHP應(yīng)用調(diào)用代碼
PHP應(yīng)用
$config = array(
'host' =>'211.*.*.*', //服務(wù)器
'port' => '23', //端口
'username' =>'test', //用戶名
'password' =>'*****', //密碼
);
$ftp = new Sftp($config);
$localpath="E:/www/new_20170724.csv";
$serverpath='/new_20170724.csv';
$st = $ftp->upftp($localpath,$serverpath); //上傳指定文件
if($st == true){
echo "success";
}else{
echo "fail";
}
PHP應(yīng)用SFTP 封裝類
PHP應(yīng)用
<?php
/**
* SFtp上傳下載文件
*
*/
namespace Common\ORG\Util;
class Sftp
{
// 初始配置為NULL
private $config = NULL;
// 連接為NULL
private $conn = 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']))
{
}else{
echo "無法在服務(wù)器進(jìn)行身份驗(yàn)證";
}
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù)
public function downftp($remote, $local)
{
$ressftp = ssh2_sftp($this->conn);
return copy("ssh2.sftp://{$ressftp}".$remote, $local);
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,寫入ftp服務(wù)器數(shù)據(jù)
public function upftp( $local,$remote, $file_mode = 0777)
{
$ressftp = ssh2_sftp($this->conn);
return copy($local,"ssh2.sftp://{$ressftp}".$remote);
}
}
PHP應(yīng)用以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/424.html