clearstatcache()

创建于 2024-12-03 / 45
字体: [默认] [大] [更大]
❮ PHP 文件系统参考手册

实例

输出文件大小,截断文件,清除缓存,然后再次输出文件大小:

<?php
//output filesize
echo filesize("test.txt");
echo "<br />";

$file = fopen("test.txt", "a+");
// truncate file
ftruncate($file,100);
fclose($file);

//Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>

以上代码的输出:

792
100

定义和用法

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(clear_realpath_cache, filename)

参数值

参数 描述
clear_realpath_cache 可选。 指示是否清除真实路径缓存。 默认为FALSE,表示不清除realpath缓存
filename 可选。指定文件名,并仅清除该文件的实际路径和缓存

提示和注释

提示: 正在缓存的函数:



技术细节

返回值: Nothing
PHP 版本: 4.0+
PHP 更新日志: PHP 5.3 - 添加了两个可选参数:clear_realpath_cahefilename

❮ PHP 文件系统参考手册
0 人点赞过