How to Download Most Recent FTP Files Via PHP

A simple PHP script that allows you to download the most recent FTP files from your server.

Want to download your latest FTP files via PHP? Using this php script can help you download the most recent FTP files using plain PHP. 

// connect
$conn = ftp_connect('');
ftp_login($conn, 'user', 'pass');

// get list of files on given path
$files = ftp_nlist($conn, '');

$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, "target.txt", $mostRecent['file'], FTP_ASCII);

StockOverflow Source: