Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

clearstatcache()

Example

Display the file size, truncate the file, clear the cache, and then display the file size once more.

<?php
//output filesize
echo filesize(“test.txt”);
echo “<br />”;

$file = fopen(“test.txt”“a+”);
// truncate file
ftruncate($file,100);
fclose($file);

//Clear cache and check filesize again
clearstatcache();
echo filesize(“test.txt”);
?>

The result of the code above might be:

792
100

Definition and Usage

The clearstatcache() function clears the file status cache.

 

PHP caches information for certain functions to improve performance. If you need to check a file multiple times within a script and want to avoid cached results to ensure accuracy, you should use the clearstatcache() function.

Syntax

clearstatcache(clear_realpath_cachefilename)

Parameter Values

 

Parameter

Description

clear_realpath_cache

Optional. Specifies whether to clear the realpath cache. By default, this is set to FALSE, meaning the realpath cache is not cleared.

filename

Optional. Specifies a filename and clears the realpath and cache for that particular file only.

Tips and Notes

Tip: Functions that use caching include:

  • 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() 

Technical Details

Return Value:

Nothing

PHP Version:

4.0+

PHP Changelog:

PHP 5.3 introduced two optional parameters: clear_realpath_cache and filename.