Jump to Navigation

045 - How to use clearstatcache(), stat()

When you use stat(), istat(), or any of the other functions listed in the affected functions list ,PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. You can use the clearstatcache() function to clear the information that PHP caches about a file.

Example:

<?php
$file = 'test.txt';
function get_owner($file)
{
$stat = stat($file);
$user = posix_getpwuid($s['uid']);
return $user['name'];
}
$format = "UID @ %s: %s\n";
printf($format, date('r'), get_owner($file));
chown($file, 'user1');
printf($format, date('r'), get_owner($file))
clearstatcache();
printf($format, date('r'), get_owner($file));
?>

posix_getpwuid() is not working in Windows OS Server.

stat() gathers the statistics of the file named by filename. If filename is a symbolic link,
statistics are from the file itself, not the symlink.

stat() return values:

Numeric Associative Description
0 dev device number
1 ino inode number
2 mode inode protection mode
3 nlink number of links
4 uid userid of owner
5 gid group of owner
6 rdev device type, if inode device
7 size size of bytes
8 atime time of last access (UNIX timestamp)
9 mtime time of last modification (UNIX timestamp)
10 ctime time of last inode change (UNIX timestamp)
11 blksize blocksize of filesystem IO
12 block number of block allocated


Main menu 2

Story | by Dr. Radut