微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 =“$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}")

原文地址:https://www.jb51.cc/php/134538.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐