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.
// connect and login to FTP server
$ftp_server = "ftp server details";
$ftp_username = 'ftp user';
$ftp_userpass = 'ftp pass';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
//hard coded file name. I want to get only .csv file (It is just one file)
$file = "/MYDATA/2020_10_27_15_00.csv";
date_default_timezone_set('Asia/Kolkata');
$currenttime = date( 'F d Y', strtotime( 'now' ) );
$lastchanged = ftp_mdtm($ftp_conn, $file);
if ($lastchanged != -1)
{
$lastmodifieddate = date("F d Y", $lastchanged);
if (strcmp($currenttime, $lastmodifieddate) == 0)
{
echo "$file was last modified on : " . date("F d Y", $lastchanged);
//download the file
}
else
{
echo "No file for current date";
//nothing to download
}
}
else
{
echo "Could not get last modified";
}
// close connection
ftp_close($ftp_conn);
?>
How to get filename only for .csv extension with last modified date as the current date and download the file from FTP?
Thanks!