从FTP获取文件列表并下载特定文件

时间:2016-06-18 14:51:18

标签: php ftp ftps

我想每天从FTP服务器下载一些文件,我不知道确切的文件名。文件名的结构如下:Report-date-time.txt

报告是静态的,日期是可预测的(昨天),但时间是不幸的动态和不可预测的。

我可以得到一个清单:

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

我可以下载我知道文件名的文件:

$content = file_get_contents("ftps://user:pass@server/folder/file.txt");
file_put_contents("/location/file.txt", $content);

我的问题是:我如何使用该列表仅下载特定文件?

更新:转储列表

array(22) { [0]=> string(41) "Report-important-20160613_134253.txt" [1]=> string(41) "Report-important-20160614_102834.txt" [2]=> string(41) "Report-important-20160615_112745.txt" [3]=> string(41) "Report-important-20160616_082453.txt" [4]=> string(41) "Report-important-20160617-034253.txt" [5]=> string(41) "Report-important-20160618_142314.txt" [6]=> string(40) "Time-20151126-152543.xls" [7]=> string(58) "Extra-7d-20151210-135825.xls" [8]=> string(58) "Report7d-20151215-110002.csv" [9]=> string(62) "ReportPO-7d-20151210-151636.xls" [10]=> string(62) "ReportPO-7d-20151213-210514.xls" [11]=> string(62) "ReportPO -7d-20151214-074404.xls" [12]=> string(62) "ReportPO -7d-20151215-075319.xls" [13]=> string(62) "ReportPO -7d-20151216-080059.csv" [14]=> string(62) "ReportPO -7d-20151217-075519.csv" [15]=> string(62) "ReportPO -7d-20151218-075655.csv" [16]=> string(62) "ReportPO -7d-20151219-080027.csv" [17]=> string(62) "ReportPO -7d-20151220-075659.csv" [18]=> string(62) "ReportPO -7d-20151221-075837.csv" [19]=> string(62) "ReportPO -7d-20151222-074652.csv" [20]=> string(62) "ReportPO -7d-20151223-081857.csv" [21]=> string(68) "ReportTa-20151127-095630.xls" }

我每天都需要 Report-important-date_time.txt 。因为时间是可变的,我不能安排简单的下载,因为我首先要知道文件名是什么。

所以这样的事情不会起作用:

$contents = file_get_contents("sftp://user:pass@server:22/Report-important-" . date('Ymd',strtotime(-1 days)) . ".txt");

file_put_contents("/location/Report-important-" . date('Ymd',strtotime(-1 days)) . ".txt", $contents);

我认为不可能下载像

这样的名字
Report-important-20160617_*.txt 

所以我正在寻找一种获取正确文件名的方法。

1 个答案:

答案 0 :(得分:1)

下载了这样的文件列表后:

$myList = ['0'  => 'Report-important-20160613_134253.txt', 
           '1'  => 'Report-important-20160614_102834.txt', 
           '2'  => 'Report-important-20160615_112745.txt', 
           '3'  => 'Report-important-20160616_082453.txt', 
           '4'  => 'Report-important-20160617-034253.txt', 
           '5'  => 'Report-important-20160618_142314.txt', 
           '6'  => 'Time-20151126-152543.xls', 
           '7'  => 'Extra-7d-20151210-135825.xls', 
           '8'  => 'Report7d-20151215-110002.csv', 
           '9'  => 'ReportPO-7d-20151210-151636.xls', 
           '10' => 'ReportPO-7d-20151213-210514.xls', 
           '11' => 'ReportPO -7d-20151214-074404.xls', 
           '12' => 'ReportPO -7d-20151215-075319.xls', 
           '13' => 'ReportPO -7d-20151216-080059.csv', 
           '14' => 'ReportPO -7d-20151217-075519.csv', 
           '15' => 'ReportPO -7d-20151218-075655.csv', 
           '16' => 'ReportPO -7d-20151219-080027.csv', 
           '17' => 'ReportPO -7d-20151220-075659.csv', 
           '18' => 'ReportPO -7d-20151221-075837.csv', 
           '19' => 'ReportPO -7d-20151222-074652.csv', 
           '20' => 'ReportPO -7d-20151223-081857.csv', 
           '21' => 'ReportTa-20151127-095630.xls'];

你知道很多。最新文件以'Report-important-20160618'开头,即'Report-important-'.date('Ymd')。因此,您只需查看数组,然后获取匹配的文件:

foreach ($myList as $filename) {
  if (strpos($filename,'Report-important-'.date('Ymd')) !== FALSE) {
   <... download $filename ...>
}

换句话说:您不需要知道文件中的时间来获取特定日期的文件。