file_exists works inconsistently?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need
to detect if a file exists, and if so, unlink it.
I am mystified as to why the file_exists() function does not work in
one particular section of code, yet does work in another section. The
unlink() function similarly works in one section, but not the other.

I have read other posts in this group where others have had trouble
with the file_exists() function, but none seemed to help with my
problem; so here goes...

Here is the code snippet that does not work, which includes a test to
verify if 'safe mode' is off:

    if ($debug) {
        echo "<br>fullname of target file = ".$fullname;
        echo "<br>File exists? ";
        if (file_exists($fullname)) {
            echo "YES!";
        } else {
            echo "NO!!";
        echo "<br>Safe mode on? ";
        if (ini_get('safe_mode')) {
            echo "YES!";
        } else {
            echo "NO!!";
        echo "<br>(exiting...)";

Here is the (partial) output:

fullname of target file = /Net/delta/home/ehringer/MAELab/RickLIMStest/
File exists? NO!!
Safe mode on? NO!!

By cut-&-paste into a console window with ls -la command, I verify the
full pathname is correct.

If anyone has any ideas about this, I would be most grateful...

Rick Casey

Re: file_exists works inconsistently?

Quoted text here. Click to load it

Try the new PHP4 version there are many bugfixes in file_exists.
Do the same test but with is_file(). Did you find the file with the


Re: file_exists works inconsistently?

rickcasey wrote:

Quoted text here. Click to load it

1.) add a trim:
Quoted text here. Click to load it
< if (file_exists(trim($fullname))) {
to make sure there are no whitespaces in $fullname, so that you actually
test for two different paths.
2.) any fancy encodings around ? Probably not, but if so that might affect
interpretation of paths
3.) what about the permissions of containing folders ? everything readable
and the web server is allowed to change into _all_ containing folders ?
(guessing that you run the script under a web servers process...)


Re: file_exists works inconsistently?

Quoted text here. Click to load it

So you run file_exists on the same file twice in your code without
calling clearstatcache() - I suspect you should RTFM for both


Re: file_exists works inconsistently?

Read the docs for clearstatcache()

This may help understanding of PHP's somewhat dangerous file activities
for the unwary.

I can't demonstrate proof but I also suspect system/proxy caches also cause
problems linked to this behaviour.

Most people seem to ignore this so if you are using third party classes you may
want to trawl through their code to see if problems could occur from there also.

Site Timeline