清除檔案狀態快取

clearstatcache

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

clearstatcache清除檔案狀態快取

說明

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

當使用 stat()lstat() 或者任何列在受影響函式表(見下面)中的函式時,PHP 將快取這些函式的返回資訊以提供更快的效能。然而在某些情況下,你可能想清除被快取的資訊。例如如果在一個指令碼中多次檢查同一個檔案,而該檔案在此指令碼執行期間有被刪除或修改的危險時,你需要清除檔案狀態快取。這種情況下,可以用 clearstatcache() 函式來清除被 PHP 快取的該檔案資訊。

必須注意的是,對於不存在的檔案,PHP 並不會快取其資訊。所以如果呼叫 file_exists() 來檢查不存在的檔案,在該檔案沒有被建立之前,它都會返回 false。如果該檔案被建立了,就算以後被刪除,它都會返回 true 函式 unlink() 會自動清除該快取.

注意:

本函式快取特定檔名的資訊,因此只在對同一個檔名進行多次操作並且需要該檔案資訊不被快取時才需要呼叫 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()

參數

clear_realpath_cache

是否清除真實路徑快取

filename

清除檔名指定的檔案的真實路徑快取; 只在 clear_realpath_cachetrue 時啟用

返回值

沒有返回值。

更新日誌

版本 說明
5.3.0 增加了可選的 clear_realpath_cachefilename 參數.

範例

示例 #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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *