我需要修改此脚本以仅检索具有昨天日期的文件。这就是我所拥有的:
<?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);
当我运行脚本时,它抓取了我服务器上尚未存在的所有文件,这不是我想要的。我只想让它在昨天的日期获取所有文件。 感谢
答案 0 :(得分:0)
这是一个快速而肮脏的解决方案,请注意,在比较mtimes时,这不会将服务器时区差异或其他陷阱考虑在内。
主要的变化是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);