通过 sftp 从远程服务器下载 PHP

标签 php file 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 ="$remote_dir";是错误的,老实说,当我想要整个目录时,我得到了 $filename,这就是我目前所拥有的。

最佳答案

下面是一段关于如何读取文件夹并下载其所有文件的小代码:

<?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}")

关于通过 sftp 从远程服务器下载 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17687607/

相关文章:

php - CakePHP 中没有 SQL 模型

php - 为什么我不应该在 php 中使用 unix 命令?

linux - mhddfs 不支持文件拆分.. 如果文件大小超过单个存储设备的限制

c - 不使用 remove() 函数如何在 C 程序中删除文件

python - 在远程 SFTP 服务器上解压 tar.gz

c++ - 使用 libssh 从 SFTP 服务器下载文件并使用 C++ 将其写入 ofstream?

php - 错误 404 : Page not found in codeigniter when controller is in subfolder

带有 AES_DECRYPT 的 Php mysql LOWER 函数

Java - 无法创建具有 777 权限的目录(取而代之的是 775)

iphone - iPhone 的 SFTP 库?