为什么 stat() 不显示正确的 mtime 值?

why stat() does not show correct value of mtime?

我创建了带有循环的简单测试函数。它有很多回声,因为我想知道到底发生了什么。注意,循环结束时有一个 die 命令,所以它不是真正的循环。我用 test("523",$p); 调用该函数。这会创建变量 $fname = "523.txt" 并且 $p 可以设置为 1;我发现当我触摸文件并将其设置为一些 mtime、atime、10、1...等值时,stat() 没有 return 正确的值。但是,如果我删除该函数(仅保留内部代码),那么 stat() 将 return 更正值 10 1; ...任何想法我的功能可能出了什么问题?

function test($n, $p){
  $fname = "$n.txt";
  echo "<h4>$n at ".time()."</h4>";
  for ($i = 0; $i<50; $i++ ){
    $start = microtime(true);

    $st = stat("$fname");
    echo "; 1) previous access by ".$st['mtime']." ".$st['atime']."; ";

    $fsize = filesize($fname);
    if ($fsize === 0)
     echo "! The fsize is 0; ";    
    else
      {
      $fp = fopen($fname, "r");
      $locked = flock($fp, LOCK_SH);
       $s = fread($fp, $fsize );
       $success = flock($fp, LOCK_UN);
       if ( $success === false  )
         die("; r flock release failed; ");
       $success = fclose($fp);
       if ( $success === false  )
         die("; fclose failed; ");
       // 10 - data načtená , $p - prohlížeč
       if ( $success )
         { 
         $result = touch("$fname",10,$p);
         echo "; TOUCH: $result;";
         }
       if ( strlen($s)<60 ) 
          echo "*$s LENGTH:".strlen($s)."<br>";
      }
    $st = stat("$fname");
    echo "; 2) previous access by ".$st['mtime']." ".$st['atime']."; ";
die;
  }
}

查看行:

$result = touch("$fname",10,$p);

$st = stat("$fname");

结果; 1) previous access by 1570715111 1570715111; ; TOUCH: 1;; 2) previous access by 1570715111 1570715111;

引自此处:https://www.php.net/manual/en/function.stat.php

    Note: The results of this function are cached. See clearstatcache() for more details.

只需在第二次调用stat之前添加clearstatcache();