I'm trying to get the latest file with a specific extension from a folder via FTP. I'm using the following code to get the most recent file. But it gets the most recent file regardless of file extension.
// new connect
$conn = ftp_connect('ftp.website.com');
ftp_login($conn, 'username', 'password');
// get list of files on given path
$files = ftp_nlist($conn, '/data');
$mostRecent = array(
'time' =\> 0,
'file' =\> null
);
foreach ($files as $file) {
// get the last modified time for the file
$time = ftp_mdtm($conn, $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
ftp_get($conn, "/home/mywebsite/public_html/wp-content/uploads/data-zipped/target.zip", $mostRecent\['file'\], FTP_BINARY);
ftp_delete($conn, $mostRecent\['file'\]);
ftp_close($conn);
I would like to get specific files with specific extensions.
The files I want to get end with the following filename.add.zip
. The filename changes daily. So it could be file22.add.zip
moredata.add.zip
. But the add.zip
remains the same.
Unfortunately there are also files with the extension filename.del.zip
. So it can't just be .zip it needs to be add.zip
.
So via FTP, I want to pickup the most recent file ending in add.zip
.
Anyone have a solution? The code that I currently us only picks up the most recent file. Regardless of the file extension.