2016-06-18 132 views
0

我想每天從FTP服務器下載一些文件,但我不知道確切的文件名。文件名結構如下:Report-date-time.txt從FTP獲取文件列表並下載特定文件

報告是靜態的,日期是可預測的(昨天),但時間是不幸的動態和不可預測的。

我可以得到一個列表:

// 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:[email protected]/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" } 

的轉儲我需要報告重要-date_time.txt每天。因爲時間是可變的,我不能安排簡單的下載,因爲我首先必須知道文件名是什麼。

因此,像這樣將無法工作:

$contents = file_get_contents("sftp://user:[email protected]: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

@KIKOSoftware我添加了一個下載列表。我想使用這個文件來檢查一些日常的變化,並將它與其他幾個來源(我可以正確下載)進行比較。 – Glarp

回答

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

換句話說:你不需要知道文件的時間來獲得文件的某一特定日期。