PHP realpath_cache_get() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, realpath_cache_get() returns the entries that are currently stored in PHP's realpath cache.

PHP remembers the result of path lookups so it does not have to ask the file system again and again. This function lets you look inside that cache, which is mostly useful when you tune performance or debug odd path problems.

Syntax

Call realpath_cache_get() with the values you need. Start with this line.

PHP
$cache = realpath_cache_get();
CallWhat it does
realpath_cache_get()Returns an array with one item for every path stored in the cache.
count(realpath_cache_get())Counts how many paths are cached right now.
isset(realpath_cache_get()[$path])Checks whether a specific path is already in the cache.

Each key of the returned array is a path. Each value is an array with the keys expires, is_dir and realpath. The cache is per process, so a normal web request starts with a small cache.

Library declarations
PHP
realpath_cache_get(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayReturns an array keyed by path. Each item has expires (Unix time when the entry ends), is_dir (true for a folder) and realpath (the resolved path).

PHP Examples

Example 1 — Check That the Cache Has Entries

Call realpath() once so PHP stores a path, then read the cache.

PHP
<?php
realpath(__DIR__);

$cache = realpath_cache_get();

echo count($cache) > 0 ? 'The cache has entries' : 'The cache is empty', PHP_EOL;

How It Works

  1. realpath() resolves the current folder, and PHP saves the result in its cache.
  2. realpath_cache_get() returns every cached path as an array.
  3. count() tells us how many entries there are.
  4. Because we just resolved a path, the cache is not empty.

Example 2 — Look Up One Path in the Cache

Use the real path as an array key to find out whether it was cached and what type it is.

PHP
<?php
$key = realpath(__DIR__);

$cache = realpath_cache_get();

if (isset($cache[$key])) {
    $type = $cache[$key]['is_dir'] ? 'folder' : 'file';
    echo "Found: $key ($type)", PHP_EOL;
} else {
    echo "Not cached", PHP_EOL;
}

How It Works

  1. realpath() returns the clean path, which is also the key used inside the cache.
  2. isset() checks if that key exists in the array.
  3. The is_dir value tells us if the cached item is a folder.
  4. If the cache was cleared or disabled, the script would print Not cached instead.

Example 3 — List the First Few Cached Paths

Print a short sample of the cache to see what PHP remembers.

PHP
<?php
realpath(__DIR__);
realpath(__FILE__);

$cache  = realpath_cache_get();
$sample = array_slice($cache, 0, 3, true);

foreach ($sample as $path => $info) {
    echo $path, ' => ', $info['is_dir'] ? 'folder' : 'file', PHP_EOL;
}

How It Works

  1. Two realpath() calls make sure at least a few entries exist.
  2. array_slice() with true keeps the path keys while taking only the first three items.
  3. foreach reads each path and its info array.
  4. The script prints the path and whether it is a folder or a file. Your list will show your own paths.

Frequently Asked Questions

It returns an array of all paths stored in PHP's realpath cache, with details such as expiry time and whether each path is a folder.
It is a memory area where PHP remembers resolved paths. This avoids many slow file system lookups and makes include and file functions faster.
No paths have been resolved yet, or the cache was cleared with clearstatcache(true). It can also be empty on some builds where the cache is turned off.
Call clearstatcache(true). The true argument clears the realpath cache as well as the normal file status cache.
Yes. The realpath_cache_ttl setting in php.ini controls the time in seconds, and realpath_cache_size controls the maximum memory.

Next: realpath_cache_size()

realpath_cache_size() returns how many bytes of memory the realpath cache is using right n.

realpath_cache_size() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful