get*.csv文件抛出SFTP.php


get *.csv files throw SFTP php

我正在尝试从SFTP远程/文件夹/下载特定名称的csv文件

  • 123-2015-11-03.csv
  • 456-2015-11-03.csv
  • 789-2015-11-03.csv
  • 。。。大量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);  
    }
} 

您似乎想从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中找到更多信息。