Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php file_put_contents()影响目录中其他文件的fileatime()_Php_Wordpress_File Access_File Put Contents - Fatal编程技术网

Php file_put_contents()影响目录中其他文件的fileatime()

Php file_put_contents()影响目录中其他文件的fileatime(),php,wordpress,file-access,file-put-contents,Php,Wordpress,File Access,File Put Contents,我遇到了一个非常奇怪的错误,在过去几天里我一直试图解决它,但没有成功。我在WordPress插件中使用了一个类来创建自定义API端点。简而言之,WordPress插件点击一个外部API并使用我的缓存类缓存结果。使用api访问许多单独的项,从而生成几十个本地缓存文件 情景 在我的缓存类中,如果本地缓存已过期(比实例化时设置的过期时间早),则会再次命中API并缓存结果,如下所示: file_put_contents($this->cache, $this->data, LOCK_EX);

我遇到了一个非常奇怪的错误,在过去几天里我一直试图解决它,但没有成功。我在WordPress插件中使用了一个类来创建自定义API端点。简而言之,WordPress插件点击一个外部API并使用我的缓存类缓存结果。使用api访问许多单独的项,从而生成几十个本地缓存文件

情景 在我的缓存类中,如果本地缓存已过期(比实例化时设置的过期时间早),则会再次命中API并缓存结果,如下所示:

file_put_contents($this->cache, $this->data, LOCK_EX);
在我的WordPress插件中,我想循环浏览缓存文件并删除N天内未被访问的文件。此方法使用cron命中。我正在检查访问时间(这仍在开发中,正在打印以进行调试):

以下是目前为止的完整方法:

public static function cleanup_old_caches($days = 30) {

    // Get the files
    $files = scandir(UPLOADS_DIR);

    // Save out .txt files
    $cache_files = array();

    // Loop through everything
    foreach ( $files as $file ) {
        if ( strstr($file, '.txt') ) {
            $cache_files[] = $file;
        }
    }

    // Loop through the cache files
    foreach ( $cache_files as $file ) {

        clearstatcache();
        print($file . ': ' . date('F jS Y - g:i:s A', fileatime(UPLOADS_DIR . '/' . $file)));
        echo "\n";
        clearstatcache();

    }

    return '';

}
您会注意到,目前我有几个
clearstatcache()
调用

问题 每当创建新的缓存文件时,
fileatime()
为同一目录中的许多其他文件报告的访问时间都会更新为当前时间。这些有时表示在新缓存文件之后的一秒钟

以下是我的完整方法:

private function hit_api() {

    // Set the return from the API as the data to use
    $this->data = $this->curl_get_contents($this->api_url);

    // Store the API's data for next time
    file_put_contents($this->cache, $this->data, LOCK_EX);

}
我可以找到另一种编写清理逻辑的方法,但我担心PHP实际上正在处理这些文件中的每一个(对于一个新文件,我看到了18个中的12个)

我尝试过的事情
  • clearstatcache()
    在任何地方都绝对调用)
  • 手动执行所有
    fopen()
    fwrite()
    fflush()
    fclose()
    步骤
  • 写入在调用
    file\u put\u contents()时写入的文件名

如果有人知道这里发生了什么,我将不胜感激。

经过一周的编写测试并用
file\u put\u contents()
简单调用重新创建此问题,我找到了此问题的根源。做好准备。。。Spotlight正在为这些文件编制索引。从聚光灯中排除,删除缓存文件,重新启动,没有问题


我建议尝试将其进一步降低到绝对最小的代码,以再现这一点。但是,为什么您要使用
fileatime
(访问)而不是
filemtime
(修改)?我希望基于上次访问而不是上次修改来删除文件。没有意义。。。3个月前的文件可以有一个小时的访问时间old@charlietfl这就是我想要的逻辑,删除一段时间没有访问的文件,而不是刚才创建的文件。因为您的缓存应该是最新的,如果缓存文件经常被访问,而没有达到删除所需的最后访问时间,则不会发生这种情况。
private function hit_api() {

    // Set the return from the API as the data to use
    $this->data = $this->curl_get_contents($this->api_url);

    // Store the API's data for next time
    file_put_contents($this->cache, $this->data, LOCK_EX);

}