|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34589 Strange effects of file_exists() etc. caching
Submitted: 2005-09-21 21:51 UTC Modified: 2005-09-21 23:00 UTC
From: pjf at asn dot pl Assigned:
Status: Not a bug Package: *Directory/Filesystem functions
PHP Version: 5.0.5 OS: GNU
Private report: No CVE-ID: None
 [2005-09-21 21:51 UTC] pjf at asn dot pl
I find caching of file_exists() and similar functions 
causing strange results - as presented by attached code, 
it might cause unexistent files to... "exist" :). 

Reproduce code:
[pjf@boogoo file_exists]$ ls
[pjf@boogoo file_exists]$ cat file_exists.php

echo file_exists("foobar") . "\n";

echo file_exists("foobar") . "\n";


Expected result:
[pjf@boogoo file_exists]$ php file_exists.php 

Actual result:
[pjf@boogoo file_exists]$ php file_exists.php 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-21 21:55 UTC] pjf at asn dot pl
I made mistake in the code - here is the corrected  
echo file_exists("foobar") . "\n"; 
echo file_exists("foobar") . "\n";
 [2005-09-21 22:02 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Use clearstatcache() to clear the cache.
 [2005-09-21 22:21 UTC] pjf at asn dot pl
Don't you think that the key that is used to access the 
cache should be the _full_, _absolute_ path instead of 
just the string passed to file_exists()? 
Current solution doesn't guarantee proper results - how do 
you know whether the code you call doesn't change into 
some other directory and stats() eg. "foo" won't interfere 
with the other piece code checking for "foo" existence in 
other directory? The only way to do such checks reliably 
is to call clearstatcache() _each_ _time_ what makes the 
cache useless. At least for me. I think it's a design 
 [2005-09-21 22:33 UTC]
No, I don't think so.
If you're trying to outsmart the cache jumping from/into different directories and doing the stat() using *relative* path - this is not PHP problem (and it can be easily solved with clearstatcache(), but I've already told you that).
 [2005-09-21 22:52 UTC] pjf at asn dot pl
To "outsmart the cache"... Rest in piece, Unix principles. 
Hint for those who encounter the same problem as mine: use 
realpath(). A hack, but works as file_exists() should.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 05:01:28 2024 UTC