PHP SFTP应用(二)
SFTP应用(二)
FTP进行文件传输需要通过端口进行。一般所需端口为:
- 控制链路—TCP端口21。控制器端。用于发送指令给服务器以及等待服务器响应。
 - 数据链路---TCP端口20。数据传输端口。用来建立数据传输通道的。主要用来从客户向服务器发送一个文件、从服务器向客户发送一个文件、从服务器向客户发送文件或目录列表。
 
FTP为了适应不同的网络环境,支持主动连接和被动连接两种模式。这两种模式都主要针对数据链路进行的,跟控制链路无关。
FTP的安全隐患:
- FTP服务器软件漏洞。
 - 明文口令。
 - FTP旗标。
 - 通过FTP服务器进行端口扫描。
 - 数据劫持。
 
FTP的安全策略:
- 使用较比安全的系统和FTP服务软件。
 - 使用密文传输用户名和口令。
 - 更改服务软件的旗标。
 - 加强协议安全性。
 
SFTP是Secure File Transfer Protocol的缩写,是安全文件传送协议。可以为传输文件提供一种安全的加密方法。跟ftp几乎语法功能一样。
SFTP是SSH的一部分,是一种传输档案至Blogger伺服器的安全方式。它本身没有单独的守护进程,必须使用sshd守护进程来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以使用SFTP是十分安全的。但由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多。在对网络安全性要求更高时,代替FTP使用。
搭建SFTP server【我这里用的是Windows系统】
- 使用FreeSSHrd;
 - 教程自行百度(很简单的),我这里创建了用户 sun ,密码141592;地址使用127.0.0.1,端口号 28(22端口被其他应用占用了,端口号随意设置,不要冲突就好),下面我会对此测试。
 
开启ssh2扩展【我这里用的是wamp】
- 
打印phpinfo();找到你的Architectrue、Thread Safety(enabled 线程;disabled 非线程),后面会用到;
 - 
http://windows.php.net/downloads/pecl/releases/ssh2/0.12 打开找到你的对应版本下载下来;
 - 
会有三个文件,libssh2.dll、php_ssh.dll、php_ssh2.pdb。将 php_ssh.dll、php_ssh2.pdb 放到你的wamp/bin/php/ext/ 下;将libssh2.dll 复制到 c:/windows/system32 和 c:/windows/syswow64 各一份
 - 
在wamp中的 php.ini中加入 extension=php_ssh2.dll。
 - 
重启wamp,扩展打开。
 
注意
- 下载正确的ssh版本dll;
 
PHP操作实例
<?php
$strServer = "127.0.0.1";
$strServerPort = "28";
$strServerUsername = "sun";
$strServerPassword = "141592";
$csv_filename = "test.txt";
//connect to server
$resConnection = ssh2_connect($strServer, $strServerPort);
//ssh2_auth_password — Authenticate over SSH using a plain password 
if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
     echo "connected\r\n";
    
    //ssh2_sftp — Initialize SFTP subsystem 
    $resSFTP = ssh2_sftp($resConnection);    
    $resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');         //获取句柄
    fwrite($resFile, "Testing");  //写入内容
    fclose($resFile);   //关闭句柄     
    echo "success\r\n";
}else{
    echo "Unable to authenticate on server";
}
?>
SFTP操作类
<?php
class SFTPConnection
{
    private $connection;
    private $sftp;
    public function __construct($host, $port=22)
    {
        $this->connection = @ssh2_connect($host, $port);
        if (! $this->connection)
            throw new Exception("Could not connect to $host on port $port.");
    }
    public function login($username, $password)
    {
        if (! @ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " .
                "and password $password.");
            $this->sftp = @ssh2_sftp($this->connection);
            if (! $this->sftp)
                throw new Exception("Could not initialize SFTP subsystem.");
    }
    public function uploadFile($local_file, $remote_file)
    {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');  //w
        if (! $stream)
            throw new Exception("Could not open file: $remote_file");
        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false)
            throw new Exception("Could not open local file: $local_file.");
        if (@fwrite($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");
        @fclose($stream);
        return true;
    }
    
    
}
------ 天行健,君子以自强不息!

                
            
        
浙公网安备 33010602011771号