PHP clearstatcache() 函数

时间:6年前   阅读:4116

定义和用法

clearstatcache() 函数清除文件状态缓存。

clearstatcache() 函数会缓存某些函数的返回信息,以便提供更高的性能。但是有时候,比如在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存,以便获得正确的结果。要做到这一点,就需要使用 clearstatcache() 函数,清除被 PHP 缓存的该文件信息。

会进行缓存的函数,即受 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

必须注意的是,对于不存在的文件,PHP 并不会缓存其信息。所以如果调用 file_exists() 来检查不存在的文件,在该文件没有被创建之前,它都会返回 FALSE。如果该文件被创建了,就算以后被删除,它都会返回 TRUE 函数 unlink() 会自动清除该缓存.

<?php
$file = 'output_log.txt';

function get_owner($file)
{
    $stat = stat($file);
    $user = posix_getpwuid($stat['uid']);
    return $user['name'];
}

$format = "UID @ %s: %s\n";

printf($format, date('r'), get_owner($file));

chown($file, 'ross');
printf($format, date('r'), get_owner($file));

clearstatcache();
printf($format, date('r'), get_owner($file));
?>

输出:

UID @ Sun, 12 Oct 2008 20:48:28 +0100: root

UID @ Sun, 12 Oct 2008 20:48:28 +0100: root

UID @ Sun, 12 Oct 2008 20:48:28 +0100: ross

本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。

微信扫码关注

更新实时通知

上一篇:loadlibrary失败的原因和解决方法

下一篇:2020-04-13行情研判及操作策略-沪深期权

网友评论

请先 登录 再评论,若不是会员请先 注册