2011-09-14 174 views
6

PHPを使用してSFTPサーバからファイルをダウンロードしようとしていますが、ファイルをダウンロードするための正しいドキュメントが見つかりません。PHPを使用してSFTPからファイルをダウンロードする方法は?

<?php 
$strServer = "pass.com"; 
$strServerPort = "22"; 
$strServerUsername = "admin"; 
$strServerPassword = "password"; 
$resConnection = ssh2_connect($strServer, $strServerPort); 
if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)) { 
    $resSFTP = ssh2_sftp($resConnection); 
    echo "success"; 
} 
?> 

SFTP接続を開いたら、ファイルをダウンロードするために何をする必要がありますか? phpseclib, a pure PHP SFTP implementationを使用して

+1

そこで質問はありますか? –

+0

@Baszzタイトルを読んでください。 –

+1

@OZ_:私は知っている...私はそれをそのように編集した。 –

答えて

5

<?php 
include('Net/SFTP.php'); 

$sftp = new Net_SFTP('www.domain.tld'); 
if (!$sftp->login('username', 'password')) { 
    exit('Login Failed'); 
} 

// outputs the contents of filename.remote to the screen 
echo $sftp->get('filename.remote'); 
?> 
3

あなたがSFTP接続がオープンしたら、ファイルを読み込むと、このようなfopenfread、およびfwriteなどの標準的なPHPの関数を使用して書くことができます。リモートファイルを開くには、ssh2.sftp://リソースハンドラを使用するだけです。ここで

は、ディレクトリをスキャンし、ルートフォルダ内のすべてのファイルをダウンロードする例です。

// Assuming the SSH connection is already established: 
$resSFTP = ssh2_sftp($resConnection); 
$dirhandle = opendir("ssh2.sftp://$resSFTP/"); 
while ($entry = readdir($dirhandle)){ 
    $remotehandle = fopen("ssh2.sftp://$resSFTP/$entry", 'r'); 
    $localhandle = fopen("/tmp/$entry", 'w'); 
    while($chunk = fread($remotehandle, 8192)) { 
     fwrite($localhandle, $chunk); 
    } 
    fclose($remotehandle); 
    fclose($localhandle); 
} 
+0

PHP5.6以降、これはうまくいかず、失敗します:

 $remotehandle = fopen("ssh2.sftp://$resSFTP/$entry", 'r'); 
$ resSFTPを明示的にintに変換する必要があります:
 $remotehandle = fopen('ssh2.sftp://' . intval($resSFTP) . '/$entry', 'r'); 

関連する問題