PHP clearstatcache() 函數(shù)
定義和用法
clearstatcache() 函數(shù)清除文件狀態(tài)緩存。
clearstatcache() 函數(shù)會緩存某些函數(shù)的返回信息,以便提供更高的性能。但是有時(shí)候,比如在一個(gè)腳本中多次檢查同一個(gè)文件,而該文件在此腳本執(zhí)行期間有被刪除或修改的危險(xiǎn)時(shí),你需要清除文件狀態(tài)緩存,以便獲得正確的結(jié)果。要做到這一點(diǎn),就需要使用 clearstatcache() 函數(shù)。
會進(jìn)行緩存的函數(shù),即受 clearstatcache() 函數(shù)影響的函數(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()
語法
clearstatcache()
例子
<?php //檢查文件大小 echo filesize("test.txt"); $file = fopen("test.txt", "a+"); // 截取文件 ftruncate($file,100); fclose($file); //清除緩存并再次檢查文件大小 clearstatcache(); echo filesize("test.txt"); ?>
輸出:
792 100