downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

copy> <chown
Last updated: Fri, 19 Feb 2010

view this page in

clearstatcache

(PHP 4, PHP 5)

clearstatcacheファイルのステータスのキャッシュをクリアする

説明

void clearstatcache ([ bool $clear_realpath_cache = false [, string $filename ]] )

statlstat 、 またはその他の関数(後述)を使用すると、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

realpath キャッシュをクリアするか否か (デフォルトは FALSE)。

filename

realpath キャッシュを特定のファイル名にクリアする。 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



add a note add a note User Contributed Notes
clearstatcache
markandrewslade at gmail dot com
08-Mar-2010 07:54
On Linux, a forked process inherits a copy of the parent's cache, but after forking the two caches do not impact each other.  The snippet below demonstrates this by creating a child and confirming outdated (cached) information, then clearing the cache, and getting new information.

<?php

function report($directory, $prefix = '') { printf('%sDoes %s exist?  PHP says "%s"'. PHP_EOL, $prefix, $directory, is_dir($directory) ? 'yes' : 'no'); }
$target = './delete-me-before-running-statcache';

if (
is_dir($target)) {
    die(
"Delete $target before running.\n");
}

echo
"Creating $target.\n";
mkdir($target) || die("Unable to create $target.\n");
report($target); // is_dir($target) is now cached as true

echo "Unlinking $target.\n";
rmdir($target) || die("Unable to unlink $target.\n");

// This will say "yes", which is old (inaccurate) information.
report($target);

if ((
$pid = pcntl_fork()) === -1) { die("Failed to pcntl_fork.\n"); }
elseif (
$pid === 0) {
   
// child
   
report($target, '<<child>> ');
    echo
"<<child>> Clearing stat cache.\n";
   
clearstatcache();
   
report($target, '<<child>> ');
} else {
   
// parent
   
sleep(2); // move this to the child block to reverse the test.
   
report($target, '<<<parent>> ');
   
clearstatcache();
   
report($target, '<<<parent>> ');
}

?>

copy> <chown
Last updated: Fri, 19 Feb 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites