从 FTP 获取文件列表并下载特定文件
Get file list from FTP and download specific files
我想每天从 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" }
我每天需要报告重要日期_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
所以我正在寻找一种获取正确文件名的方法。
一旦您下载了这样的文件列表:
$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 ...>
}
换句话说:您不需要知道文件中的时间来获取特定日期的文件。
我想每天从 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" }
我每天需要报告重要日期_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
所以我正在寻找一种获取正确文件名的方法。
一旦您下载了这样的文件列表:
$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 ...>
}
换句话说:您不需要知道文件中的时间来获取特定日期的文件。