6.7 Flushing the Cache


You want to call the status functions ( stat() , lstat () , file_exists() , is_writeable() , is_readable() , is_executable() , is_file() , is_dir() , is_link() , filectime() , fileatime() , filemtime() , fileinode() , filegroup() , fileowner() , filesize() , filetype() , and fileperms() ), more than one time on the same file in your program, but they are cached by your server.

Technique

Use the clearstatcache() function before calling any of the previously listed status functions.

 <?php if (fileperms ($fn) < 644) {     chmod ($fn, 0644); } clearstatcache(); if (fileperms ($fn) < 644) {     die('chmod failed'); } ?> 

Comments

Because the status functions are so taxing on the system when called, the results of these functions are cached for faster access. Therefore, you must clear the stat cache with the clearstatcache() function.

Please note that results from the status functions are cached only for the lifetime of the program execution, meaning that if you call the function only once for a particular file, you do not need to clear the cache. In the following example, a clearstatcache() function call would be unnecessary.

 <?php if (file_exists($fn)) {     print "$fn exists"; } if (is_readable ($fn)) {     print "$fn is readable"; } unlink ($fn); if (file_exists ($new_file)) {    print "$new_file exists"; } ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net