python,php同步远程文件夹代码
<?php
// 连接服务器
$connection = ssh2_connect('hostname', 'port');
if (ssh2_auth_password($connection, 'username', 'password')) {echo "Connected\n";}
else {die('Connection Failed');
// 进入远程文件夹$remotepath = '/home/remote/example/';
ssh2_exec($connection, 'cd ' . $remotepath);// 同步文件夹
$sftp = ssh2_sftp($connection);// 将远程文件夹下载到本地文件夹
$localpath = '/home/local';
foreach (ssh2_scandir($sftp, $remotepath) as $filename) {
if ('.' === $filename || '..' === $filename) {continue;}
$file = "{$remotepath}{$filename}";
if (is_dir($file)) {continue;}
$stream = @fopen("ssh2.sftp://{$sftp}{$file}", 'r');
if (!$stream) {throw new \Exception("Could not open file: $file");}
$contents = '';
while (!feof($stream)) {$contents .= fread($stream, 8192);}
fclose($stream);
$destination = $localpath . DIRECTORY_SEPARATOR . $filename;
file_put_contents($destination, $contents);}
?>
版权属于:BLOG DEWEBSTUDIO 本文作者:狒狒
原文地址: http://blog.dewebstudio.com/?post=134
版权声明:转载时必须以链接形式注明原始出处及本声明。
发表评论