0

我需要修改此脚本以仅检索具有昨天日期的文件。这是我所拥有的:

<?php
$username = 'XXXXXX';
$password = 'XXXXXX';
$ftp_server = 'data.mywebsite.com';

 $conn = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
if(ftp_login($conn, $username, $password))
{
    echo 'Logged in';
}
else
{
    echo 'FTP Error:Could not log in to '.$ftp_server;
    exit();
}
ftp_pasv ($conn, true);
if (ftp_chdir($conn, "../Photos/Hi-res")) {
    echo "Current FTP directory is now: " . ftp_pwd($conn) . "\n";
} else { 
    echo "Couldn't change to Photos directory\n";
}

$list = ftp_nlist($conn, '.');
function is_img($file) {
    if(preg_match('/.*\.png/', $file))
    {
        return preg_match('/.*\.png/', $file) > 0;
    }
    if(preg_match('/.*\.jpg/', $file))
    {
        return preg_match('/.*\.jpg/', $file) > 0;
    }
    if(preg_match('/.*\.gif/', $file))
    {
        return preg_match('/.*\.gif/', $file) > 0;
    }
}

$filtered = array_filter($list, is_img);
foreach($filtered as $img) {
    if (ftp_get($conn, $img, $img, FTP_BINARY)) {
        echo "Successfully written to $img\n";
    } else {
        echo "There was a problem\n";
    }
}
ftp_close($conn);

当我运行脚本时,它会抓取我服务器上不存在的所有文件,这不是我想要的。我只想让它抓取所有具有昨天日期的文件。谢谢

4

1 回答 1

0

这是一个快速而肮脏的解决方案,请注意,在比较 mtime 时,这不会考虑服务器时区差异或其他陷阱。

主要变化是 is_img 函数已被重命名并使用 ftp_mdtm() 为每个文件扩展。

$username = 'XXXXXX';
$password = 'XXXXXX';
$ftp_server = 'data.mywebsite.com';
$now = time();

 $conn = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
if(ftp_login($conn, $username, $password))
{
    echo 'Logged in';
}
else
{
    echo 'FTP Error:Could not log in to '.$ftp_server;
    exit();
}
ftp_pasv ($conn, true);
if (ftp_chdir($conn, "../Photos/Hi-res")) {
    echo "Current FTP directory is now: " . ftp_pwd($conn) . "\n";
} else {
    echo "Couldn't change to Photos directory\n";
}

$list = ftp_nlist($conn, '.');
function is_for_download($file) {
    $is_img = false;

    if(preg_match('/.*\.png/', $file))
    {
        $is_img = preg_match('/.*\.png/', $file) > 0;
    }
    if(preg_match('/.*\.jpg/', $file))
    {
        $is_img = preg_match('/.*\.jpg/', $file) > 0;
    }
    if(preg_match('/.*\.gif/', $file))
    {
        $is_img = preg_match('/.*\.gif/', $file) > 0;
    }

    if (!$is_img) {
        return false;
    }

    global $conn;
    global $now;

    $yesterday_start = strtotime('yesterday midnight', $now);
    $yesterday_end = strtotime('yesterday midnight + 24 hours', $now);
    $mtime = ftp_mdtm($conn, $file);

    if ($yesterday_start <= $mtime && $mtime <= $yesterday_end) {
        return true;
    }
    return false;
}

$filtered = array_filter($list, 'is_for_download');
foreach($filtered as $img) {
    if (ftp_get($conn, $img, $img, FTP_BINARY)) {
        echo "Successfully written to $img\n";
    } else {
        echo "There was a problem\n";
    }
}
ftp_close($conn);
于 2012-07-18T18:42:16.167 回答