2011-09-14 6 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

@ 오즈 : 알아요. 나는 그렇게 편집했습니다. –

답변

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 연결이 열려되면, 당신은 파일을 읽고 같은 fopen, freadfwrite 같은 표준 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'); 

관련 문제