Skip to content
Advertisement

Get File from FTP server only for current date via FTP

I am trying to get a file from FTP with the last modified date as today. This works perfectly if I know the file name.

In my FTP server, the file name is dynamic. I only know the file extension which .csv and has one CSV file with many other different files.

how do I get only the .csv file which has the last modified date as the current date? Once I get it I will download that file from FTP.

This is what I tried and works fine ONLY if I know the file name.

JavaScript

How to get filename only for .csv extension with last modified date as the current date and download the file from FTP?

Thanks!

Advertisement

Answer

List the files and loop them, get the modified date of each one and compare to today’s date (not tested):

JavaScript

If there could be multiple files with today’s date then assign to an array and do something else with them:

JavaScript

NOTE: This solution will be slow especially with many files. You may look at ftp_rawlist or issue a ls or similar command with sorting using ftp_raw and then parse out the date.

Thanks to Martin Prikryl‘s comment, you might be able to do something like this, getting the file information into an multidimensional array and checking that:

JavaScript

Then you can get the name(s):

JavaScript

If ftp_mlsd doesn’t accept wildcards, then you’ll need to add checking the $v["name"] for .csv extension to the filter code.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement