【问题标题】:filemtime() constant during execution despite changes to filefilemtime() 在执行期间保持不变,尽管文件发生了变化
【发布时间】:2012-07-27 00:03:48
【问题描述】:

尝试运行

<?php
  echo filemtime("test.txt")."\n";
  sleep(4);
  file_put_contents("test.txt", "test");
  echo filemtime("test.txt")."\n";
?>

对我来说,打印的命令行:

1343490984
1343490984

这不可能吧?

【问题讨论】:

    标签: php filemtime


    【解决方案1】:

    来自filemtime documentation

    注意:此函数的结果会被缓存。详情请见clearstatcache()

    您需要先拨打clearstatcache(),然后再拨打filemtime()

    echo filemtime("test.txt")."\n";
    sleep(4);
    file_put_contents("test.txt", "test");
    clearstatcache();
    echo filemtime("test.txt")."\n";
    

    【讨论】:

    • 比我也快 5 秒 :p
    • 当编码器可以简单地将文件状态存储在变量中时,我发现 PHP 缓存文件状态是多余的。
    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2020-05-15
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多