我在Linux上运行此代码以递归方式从目录中复制文件 回声声明'发现了DIR!'从来没有打印过。我也尝试过is_dir(realpath($ file))并且它也没有用。还尝试了is_dir($ path。/。$ file)。没运气。还有其他建议吗?
<?php
$username = 'username';
$password = 'password';
/* Remote directory to get */
$remote_dir = '/home/sets';
$local_dir = '/var/www/sets';
// set up connection
$connection = ssh2_connect('1.2.3.4', 22);
//log in with username and password
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect via SSH');
// create SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to connect via SFTP');
// download all of the files in the directory
recursiveScp($remote_dir, $sftp, $connection, $local_dir);
function recursiveScp($path, $sftp, $connection, $local_dir) {
$files = scandir('ssh2.sftp://' . intval($sftp) . $path);
if (!empty($files)) {
foreach ($files as $file) {
if ($file != '.' && $file != '..'
&& ($file[0] != '.')) { // don't copy over hidden files
if (is_dir($file))
{
echo ('FOUND A DIR!' . '<br>');
// make folder
mkdir($local_dir . $file);
// recur, adding a / to the path
recursiveScp($path . '/', $sftp, $connection, $local_dir);
}
else
{
ssh2_scp_recv($connection, "$path/$file", "$local_dir/$file");
}
}
}
}
}
?>