php 用于检查上传的图像在目录中的时间是否超过一天的函数

php function to check when an uploaded image has been in the directory for longer than a day

我有一个 cron 控制器 class,基本上可以处理所有与 cron 作业相关的工作。我正在尝试向控制器添加一个功能,用于删除目录中超过一天的图像。到目前为止,这是我所拥有的,我认为这对我来说很有意义,但是当我测试它以查看它是否有效时,我收到如下错误:

警告:filemtime():统计失败 4148_1432931936_0.jpeg

警告:取消链接(public/images/gallery_images/files/4148_1432931936_0.jpeg

public function delete_gallery_images()
{
    $dir = opendir('../public/images/gallery_images/files/');

    if ($dir) {
            // Read directory contents
        while (false !== ($file = readdir($dir))) {

            if($file != "." && $file != "..") {
                // Check the create time of each file (older than 1 day)
                if (filemtime($file) < (time() - 60 * 60 * 24)) {
                    unlink('../public/images/gallery_images/files/'.$file);
                }
            }
        }
    }
//      //close dir
//      closedir($dir);
}

有什么想法吗?非常感谢你。

您的目录名称似乎不一致。试试这个:

public function delete_gallery_images()
{
    $dirName = '../public/images/gallery_images/files/';
    $dir = opendir($dirName);

    if ($dir) {
            // Read directory contents
        while (false !== ($file = readdir($dir))) {
            if($file != "." && $file != "..") {
                // Check the create time of each file (older than 1 day)
                $fname = $dirName . $file;
                if (filemtime($fname) < (time() - 60 * 60 * 24)) {
                    unlink($fname);
                }
            }
        }
    }
    //close dir
    closedir($dir);
}