如何每周获取新图像-PHP

时间:2013-11-02 01:54:54

标签: php codeigniter

与服务器有共享驱动器;在共享驱动器中,有一些图像由摄影部门定期处理。我必须编写我的网络服务并使用调度程序来调用它(假设每个星期三),如果有任何新图像(从最后一次调用(检查))我必须使用它们来显示在网站上。我对我的策略表示怀疑,我希望得到您的确认,以确保我走在正确的轨道上:

我的策略:

1)我使用php的scandir扫描驱动器以获取该特定文件夹中的所有图像 2)我有时间获取新图像,我将它们的ID放入数据库中(图像根据ID保存)。 3)下周我运行我的Web服务,我检查图像是否在数据库中。如果不添加它并将其视为新图像,...

你有更好的想法吗?

1 个答案:

答案 0 :(得分:1)

你的方法听起来不错。但是,您可以通过查看创建文件的日期在没有数据库的情况下执行此操作;假设任何文件创建的时间比上次运行检查时更新,因此在上周三之后创建的任何文件都是新文件。

$dirPath='/path/of/your/images';
$files=scandir($dirPath);
//assuming this is in fact once a week, 
//adjust '$lastCheck' based on the schedule this will run
$lastCheck=strtotime("-7 day"); 
foreach($files as $file)
{
    if (is_file("$dirPath/$file") &&  !is_link("$dirPath/$file") ) //make sure its not a directory or symlink
    {
        $createTime=filectime("$dirPath/$file");
        //check if its older than a week
        if ($createTime>$lastCheck)
        {
            //file is newer than a week
            $newFiles[]="$dirPath/$file";
        }

    }
}

//now $newFiles has all the files from this week, with no DB interaction.