0

我认为这个问题被问过几次,我已经经历过并且正在使用网络上的一些代码。现在我遇到了时间问题。

让我先提供代码:

<?php
   $pathToDir = getcwd();               

   $pathToFilesDir = $pathToDir . '\files';

   $pathToFiles = $pathToFilesDir . '\\';

    if ($handle = opendir($pathToFilesDir)) {

        while (false !== ($file = readdir($handle))) { 

            $filelastmodified = filemtime($file);

            if ($file != '.' && $file != '..'){               

                if((time() - $filelastmodified) > 60 && is_file($file)){

                    unlink($pathToFiles . $file);

                }                
            }                 
        }
        closedir($handle); 
    }

?>

这就是我正在尝试的,自动删除一分钟前的文件。仅出于测试目的,我尝试了一分钟。结果是文件没有被删除。另一方面,我刚刚删除了这个时间条件

if((time() - $filelastmodified) > 60 && is_file($file))

并运行脚本,它可以立即删除文件。

任何人都可以发现我正在犯的错误吗?

谢谢拉克斯

4

1 回答 1

0

在这里,我的代码按生命周期检查缓存,它工作正常。

if (is_file($dir.$file) && is_readable($dir)) {
    if ($lifetime) {
        @clearstatcache();
        if ((time() - @filemtime($dir.$file)) < $lifetime) {
            return file_get_contents($dir.$file); # Return the cache
        } else {
            @unlink($dir.$file); # Cache has expired
        }
    } else {
        return file_get_contents($dir.$file); # Return the cache
    }
} return null; # Cache not found
于 2012-06-07T15:02:10.133 回答