简体   繁体   English

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

[英]Get file list from FTP and download specific files

I want to daily download some files from a FTP-server of which I don't know the exact filename. 我想每天从不知道确切文件名的FTP服务器下载一些文件。 The filename is structured like: Report-date-time.txt 文件名的结构如下:Report-date-time.txt

Report is static, date is predictable (yesterday) but time is unfortunate dynamic and unpredictable. 报告是静态的,日期是可预测的(昨天),但时间是动态的且不可预测的。

I can get a list: 我可以得到一个清单:

// 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);

And i can download files for which i know the filename: 而且我可以下载我知道文件名的文件:

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

My question is: How can i use the list to download specific files only? 我的问题是:如何使用列表仅下载特定文件?

Update: Dump of the list 更新:列表转储

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" }

I need the Report-important-date_time.txt daily. 我每天需要Report-important-date_time.txt Because the time is variable i can't schedule a simple download because i first have to know what the filename is. 因为时间是可变的,所以我无法安排简单的下载,因为我首先必须知道文件名是什么。

So something like this won't work: 所以这样的事情是行不通的:

$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);

I don't think it is possible to download a name like 我认为不可能下载这样的名称

Report-important-20160617_*.txt 

so I'm looking for a way to get the correct filename. 所以我正在寻找一种获取正确文件名的方法。

Once you downloaded the list of files like this: 一旦下载了文件列表,如下所示:

$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'];

You know a lot. 你知道很多 The lastest file starts with 'Report-important-20160618' , which is: 'Report-important-'.date('Ymd') . 最新文件以'Report-important-20160618' ,即: 'Report-important-'.date('Ymd') So all you have to do is look through the array, and get thoses files that match: 因此,您要做的就是浏览数组,并获得匹配的文件:

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

In other words: You don't need to know the time in the file to get a file for a certain date. 换句话说:您不需要知道文件中的时间就可以获取某个日期的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM