mikatakana
mikatakana

Reputation: 523

How to get list of files in FTP-server since specific date (command or cURL php)

I'm need to get list of files from FTP-server which last-modified date will be later than my specific date (files which was modified from this date).

Which way will be "cheaper" for this task? Using cURL library for PHP.

Upvotes: 0

Views: 3746

Answers (1)

mikatakana
mikatakana

Reputation: 523

My version:

function since_date ($date, $folder = '')
{
    $files = [];
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL            => $folder . '/',
        CURLOPT_USERPWD        => 'user:password',
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_CUSTOMREQUEST  => 'LIST -t'
    ]);

    // Convert date to timestamp
    $limit = strtotime($date);

    // Get files list sorted by last-modification date
    if ($ls = curl_exec($curl)) {
        foreach (explode("\n", trim($ls, "\n")) as $line) {
            // Parse response line to array of values
            $line = preg_split('/\s+/', $line, 9);

            // Get each file timestamp and compare it with specified date
            if ($ts = strtotime(implode(' ', array_slice($line, -4, 3))) >= $limit) {
                $files[ end($line) ] = $ts;
            } else {
                // Got an older files...
                break;
            }
        }
    }

    curl_close($curl);
    return $files;
}

Upvotes: 1

Related Questions