file_get_contents getting wrong results

This is the desired behavior of PHP you can see this here because PHP uses realpath_cache to stores the file paths due to performance enhancements so that it can reduce Disk Operations.

In order to avoid this behavior maybe you can try to clear the realpath_cache before using the get_file_contents function

You can try something like this:


clearstatcache();
$data = file_get_contents("Your File");

You can read more for clearstatcache on PHP doc.


It's too much depend on OS level. So how about try to think out the box. How about try to read the real location of file by readlink, and use that real location path ?

$realPath = shell_exec("readlink " . $yourSymlink);
$fileContent = file_get_contents($realPath);

There are two caches.

First the OS cache and then the PHP cache.

In most of the cases clearstatcache(true) before file_get_contents(...) does the job.

But sometimes you also need to clear the OS cache. In case of Linux, there I can think of two places to clear. PageCache (1) and dentries/inodes (2).

This clears both:

shell_exec('echo 3 > /proc/sys/vm/drop_caches')

Note: This is good for troubleshooting but not for frequent calls in production as it clears the whole OS cache and costs the system a few moments of cache re-population.