My workflow is pretty simple:
clearstatcache()clearstatcache()Still, at the end, is_file() returns false for a while before deciding to return true when i refresh 10sec later for example.
It looks like a cache problem, doesn't it?
Here's a piece of my code:
// step 1
$path = 'file_to_delete.jpg';
unlink($path);
// is_file($path) returns false here -- normal behavior
// step 2
clearstatcache();
// step 3 -- some stuff going on on an uploaded image, that leads to:
imagejpeg($imagetosave, $path, 80);
// step 4
clearstatcache();
// is_file() returns false, i have to wait a couple of seconds before it starts returning true
Thank you for your help!
EDIT:
Given all the answers i had, the problem doesn't seem to come from clearstatcache().
But should i add, when i overwrite the file (thus its existing status doesn't change), is_file() returns the good result. but when its existing status actually changes, the problem happens. It would be weird if the error didn't come from clearstatcache(), right? (or something related to this cache indeed)
I agree with @hakre, check the result of the create/delete.
It's not quick-and-dirty, even Rasmus himself recommends it:
[2011-03-31 08:34 UTC] [email protected]
You guys realize that the stat cache is per-request, right? You only need to clear the stat cache before a file_exists() call if you A. did a stat for it, and B. either created or deleted it on that request. In which case you shouldn't need to stat it again since the success status of the create/delete will tell you whether the file is there or not. Perhaps for long-running daemons or something this becomes more of an issue, but for a typical web request the stat cache typically saves you dozens of system calls.
If you really insist on using clearstatcache(), the only thing (other than write caching) coming to my mind is, that you mave have a very huge upload folder, containing thousands of files.
Having many, many files in a single folder will definitely slow down re-stating.
If so, try to reduce the number of files to gain stat performance, e.g. by creating multiple folders, one folder for each initial char of your upload filenames, like upload/a/, upload/b/, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With