clearstatcache

(PHP 4, PHP 5, PHP 7, PHP 8)

clearstatcache清除文件狀態(tài)緩存

說明

clearstatcache(bool $clear_realpath_cache = false, string $filename = ?): void

當使用 stat(),lstat() 或者任何列在受影響函數(shù)表(見下面)中的函數(shù)時,PHP 將緩存這些函數(shù)的返回信息以提供更快的性能。然而在某些情況下,你可能想清除被緩存的信息。例如如果在一個腳本中多次檢查同一個文件,而該文件在此腳本執(zhí)行期間有被刪除或修改的危險時,你需要清除文件狀態(tài)緩存。這種情況下,可以用 clearstatcache() 函數(shù)來清除被 PHP 緩存的該文件信息。

必須注意的是,對于不存在的文件,PHP 并不會緩存其信息。所以如果調(diào)用 file_exists() 來檢查不存在的文件,在該文件沒有被創(chuàng)建之前,它都會返回 false。如果該文件被創(chuàng)建了,就算以后被刪除,它都會返回 true 函數(shù) unlink() 會自動清除該緩存.

注意:

本函數(shù)緩存特定文件名的信息,因此只在對同一個文件名進行多次操作并且需要該文件信息不被緩存時才需要調(diào)用 clearstatcache()

受影響的函數(shù)包括 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()。

參數(shù)

clear_realpath_cache

是否清除真實路徑緩存

filename

清除文件名指定的文件的真實路徑緩存; 只在 clear_realpath_cachetrue 時啟用

返回值

沒有返回值。

更新日志

版本 說明
5.3.0 增加了可選的 clear_realpath_cachefilename 參數(shù).

范例

示例 #1 clearstatcache() 例子

<?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($formatdate('r'), get_owner($file));

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

clearstatcache();
printf($formatdate('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