简体   繁体   中英

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. The filename is structured like: 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. 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') . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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