代码之家  ›  专栏  ›  技术社区  ›  profimedica

PHP检查文件是否更新[重复]

  •  0
  • profimedica  · 技术社区  · 6 年前

    我正在尝试获取一个文件在我使用fwrite写入之前和之后的最后修改时间。但是,出于某种原因,我得到了相同的值。

    <?php
    $i = filemtime('log.txt');
    echo gmdate("h:i:s", $i);
    echo "<br/>";
    $e=fopen('log.txt', 'w');
    fwrite($e, "well well well");
    $j = filemtime('log.txt');
    echo gmdate("h:i:s", $j);
    
    ?>
    

    现在我修改日志。在我运行这个脚本前大约一分钟,用一个文本编辑器。所以我应该得到大约40-60秒的时差。如果有人能指出这里发生了什么,我将不胜感激。谢谢

    0 回复  |  直到 10 年前
        1
  •  6
  •   The fourth bird    10 年前

    文件 filemtime 声明缓存此函数的结果。也许你可以试一下 clearstatcache :

    <?php
    $i = filemtime('log.txt');
    echo gmdate("h:i:s", $i);
    echo "<br/>";
    $e=fopen('log.txt', 'w');
    fwrite($e, "well well well");
    clearstatcache();
    $j = filemtime('log.txt');
    echo gmdate("h:i:s", $j);
    
        2
  •  1
  •   Frank Rem    10 年前

    尝试在fwrite之后添加fclose:

    <?php
    $i = filemtime('log.txt');
    echo gmdate("h:i:s", $i);
    echo "<br/>";
    $e=fopen('log.txt', 'w');
    fwrite($e, "well well well");
    fclose($e);
    $j = filemtime('log.txt');
    echo gmdate("h:i:s", $j);
    ?>
    
    推荐文章