PHP clearstatcache() 函數

定義和用法

clearstatcache() 函數清除文件狀態緩存。

clearstatcache() 函數會緩存某些函數的返回信息,以便提供更高的性能。但是有時候,比如在一個腳本中多次檢查同一個文件,而該文件在此腳本執行期間有被刪除或修改的危險時,你需要清除文件狀態緩存,以便獲得正確的結果。要做到這一點,就需要使用 clearstatcache() 函數。

會進行緩存的函數,即受 clearstatcache() 函數影響的函數:

  • stat()
  • lstat()
  • file_exists()
  • is_writable()
  • is_readable()
  • is_executable()
  • is_file()
  • is_dir()
  • is_link()
  • filectime()
  • fileatime()
  • filemtime()
  • fileinode()
  • filegroup()
  • fileowner()
  • filesize()
  • filetype()
  • fileperms()

語法

clearstatcache()

實例

<?php
//檢查文件大小
echo filesize("test.txt");
$file = fopen("test.txt", "a+");
// 截取文件
ftruncate($file,100);
fclose($file);
//清除緩存并再次檢查文件大小
clearstatcache();
echo filesize("test.txt");
?>

輸出:

792
100