2013-07-16 58 views
6

这可能之前已经问过,我是PHP新手,并且尽力学习尽可能多,但这实际上已经抛出了我。通过sftp从远程服务器下载PHP

基本上我想知道的是,我将如何使用PHP代码将它从远程服务器下载到本地位置。它可以下载所有的文件,而不仅仅是一个文件。所以请有人向我展示/解释我将如何做到这一点?

在此先感谢。

我有什么到目前为止

<?php 
$connection - ssh2_connect('example.com', 22); 
ssh2_auth_password($connection, 'username', 'password'); 

$remote_dir="/remote_dir/"; 
$local_dir="/local_dir/"; 

$remote ="$remote_dir"; 
$stream = ssh2_exec($connection, $remote); 
stream_set_blocking($stream,true); 
$command=fread($stream,4096); 

$array=explode(\n,$command); 

$total_files=sizeof($array); 

for($i=0;$i<$total_files;$i+++){ 
    $file_name=trim($array[$i]); 
    if($file_name!=''{ 
     $remote_file=$remote_dir.$file_name; 
     $local_file=$local_dir.$file_name; 

     if(ssh2_scp_recv($connection, $remote_file,$local_file)){ 
      echo "File ".$file_name." was copied to $local_dir<br />"; 
     } 
    } 
} 
fclose($stream); 
?> 

我觉得我的$远程= “$ remote_dir”;是错误的,说实话我有$文件名,当我想要整个目录,这是迄今为止我所有。

+3

发布您到目前为止所做的事情,并且不要忘记更换任何敏感信息(如果有)。 – Prix

+0

刚刚添加了迄今为止我所拥有的。就像我说的我不知道它是对的还是不是只在几周前开始使用PHP,但我可能在我的头上。 – user2589167

+0

已发布样本希望你喜欢,因为你是新来的Stackoverflow [**我也建议你去旅行到Stackoverflow之旅,以了解如何最好地使用这个网站,这将帮助你很大程度上未来的问题。** ](http://stackoverflow.com/about) – Prix

回答

11

这里是如何读取文件夹并下载其所有的文件一个小代码:

<?php 
$host = 'localhost'; 
$port = 22; 
$username = 'username'; 
$password = 'password'; 
$remoteDir = '/must/be/the/complete/folder/path'; 
$localDir = '/can/be/the/relative/or/absolute/local/path'; 

if (!function_exists("ssh2_connect")) 
    die('Function ssh2_connect not found, you cannot use ssh2 here'); 

if (!$connection = ssh2_connect($host, $port)) 
    die('Unable to connect'); 

if (!ssh2_auth_password($connection, $username, $password)) 
    die('Unable to authenticate.'); 

if (!$stream = ssh2_sftp($connection)) 
    die('Unable to create a stream.'); 

if (!$dir = opendir("ssh2.sftp://{$stream}{$remoteDir}")) 
    die('Could not open the directory'); 

$files = array(); 
while (false !== ($file = readdir($dir))) 
{ 
    if ($file == "." || $file == "..") 
     continue; 
    $files[] = $file; 
} 

foreach ($files as $file) 
{ 
    echo "Copying file: $file\n"; 
    if (!$remote = @fopen("ssh2.sftp://{$stream}/{$remoteDir}{$file}", 'r')) 
    { 
     echo "Unable to open remote file: $file\n"; 
     continue; 
    } 

    if (!$local = @fopen($localDir . $file, 'w')) 
    { 
     echo "Unable to create local file: $file\n"; 
     continue; 
    } 

    $read = 0; 
    $filesize = filesize("ssh2.sftp://{$stream}/{$remoteDir}{$file}"); 
    while ($read < $filesize && ($buffer = fread($remote, $filesize - $read))) 
    { 
     $read += strlen($buffer); 
     if (fwrite($local, $buffer) === FALSE) 
     { 
      echo "Unable to write to local file: $file\n"; 
      break; 
     } 
    } 
    fclose($local); 
    fclose($remote); 
} 

您也可以恢复此代码(它不会复制目录)

while (false !== ($file = readdir($dirHandle))) 
{ 
    if ($file == "." || $file == "..") 
     continue; 

    echo "Copying file: $file\n"; 
    if(!ssh2_scp_recv($connection, $remoteDir . $file, $localDir . $file)) 
     echo "Could not download: ", $remoteDir, $file, "\n"; 
} 

如果不使用遥控器上的文件夹的完整路径是行不通:

opendir("ssh2.sftp://{$stream}{$remoteDir}") 
+0

好吧,看起来我没有近在咫尺,感谢这个 – user2589167

+0

if(!$ remote = @fopen(“ssh2.sftp:// {$ stream}/{$ remoteDir}/{$ file}“,'r'))//注意斜线 –

0

更新:我很好地纠正,这不使用sftp,而是使用ftps。这里有一个讨论using PHP to do SFTP的Stackoverflow链接。

PHP docs已经涵盖了你应该为此需要的大部分。下面是在远程目录获取的内容列表的例子:

<?php 
// set up basic connection 
$ftp_server = "example.com"; 
$conn_id = ftp_ssl_connect($ftp_server); 

// login with username and password 
$ftp_user_name = "myuser"; 
$ftp_user_pass = "mypass"; 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($conn_id, true); 
// check connection 
if ((!$conn_id) || (!$login_result)) { 
     echo "FTP connection has failed!"; 
     echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
     exit; 
    } else { 
     echo "Connected to $ftp_server, for user $ftp_user_name"; 
    } 

$buff = ftp_rawlist($conn_id, '.'); 
var_dump($buff); 
ftp_close($conn_id); 
?> 
+0

感谢您的快速回复!因此,使用这个我可以检查目录中所有内容的列表,并且使用ftp_ssl与使用SFTP相同? – user2589167

+0

是的。这会给你一个目录中所有文件的列表,并且你使用的是SSL SFTP –

+1

不,它们不是一回事,SFTP是SSH文件传输协议,而FTP over SSL也被称为FTP安全和FTP-SSL,这里有一篇很好的文章https://www.eldos.com/security/articles/4672.php – Prix