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.

// 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!

Advertisement

Answer

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

$files = ftp_nlist($ftp_conn, "/MYDATA/*.csv");
$today = date("Ymd");

foreach($files as $file) {
    $mtime = date("Ymd", ftp_mdtm($ftp_conn, $file));

    if($mtime == $today) {
        $found = $file;
        break;  // if there is only one or you only want one
    }
}

if(isset($found)) {
    echo "$found is today's date $today!";
    // download $found
} else {
    echo "No file found for today's date $today!";
}

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

if($mtime == $today) {
    $found[] = $file;
}

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:

$files = ftp_mlsd($conn_id, "/MYDATA/*.csv");
$today = date("Ymd");

$found = array_filter($files, function($v) use($today) {
                                  return substr($v["modify"], 0, 8) == $today;
                              });

Then you can get the name(s):

$found = array_column($found, "name");

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