获取* .csv文件抛出SFTP php

时间:2015-12-04 10:07:28

标签: php csv sftp phpseclib

我正在尝试从SFTP remote / folder /

下载特定名称的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);  
    }
} 

1 个答案:

答案 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中找到更多信息。