我正在尝试从SFTP remote / folder /
下载特定名称的csv文件这是php代码:
include('Net/SFTP.php');
$local_directory = 'D:/';
$remote_directory = '/remote/folder/';
$sftp = new Net_SFTP('xxx.xxx.xxx.xxx');
if (!$sftp->login('123', '123')) {
exit('Login Failed');
}
$files_to_upload = array(glob( '*2015-11-03.csv') ) ;
if ($handle = opendir($local_directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files_to_upload[] = $file;
}
}
closedir($handle);
}
if(!empty($files_to_upload)) {
foreach($files_to_upload as $file) {
$success = $sftp->get($remote_directory . $file,$local_directory . $file, NET_SFTP_LOCAL_FILE);
}
}
答案 0 :(得分:0)
您似乎要从SMTP服务器下载所有csv文件(以' 2015-11-03'结尾)。您可以使用以下代码。
include('Net/SFTP.php');
$sftp = new Net_SFTP('ip:port');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
$fileList = $sftp->nlist('remote_folder', FALSE);
foreach ($fileList as $file) {
//check if its a file or not
if ($sftp->is_file('remote_folder/'. $file)) {
//check filename ends with 2015-11-03.csv
if ((substr($file, -14)) == "2015-11-03.csv"){
$sftp->get('remote_folder/'. $file, $file);
echo $file. " downloading finished. <br />";
}
}
}
您可以在phpseclib中找到更多信息。