28

I am writing an PHP CLI app which loads YAML files. When trying to do this within an Xdebug session:

if (file_exists(__DIR__ . '/../../foo/bar')
{
    /* ... */
}

__DIR__ allways is xdebug: which will allways lead to false from file_exists().

Is there any work around?

yivi
  • 42,438
  • 18
  • 116
  • 138
Jan P.
  • 3,261
  • 19
  • 26

3 Answers3

15

Set $dir = __DIR__; and use if (file_exists($dir . '/../../foo/bar'). It will work like that.

Zsolti
  • 1,571
  • 1
  • 11
  • 22
0

The thing is that your debugger shows you a wrong value as DIR has already been replaced in your script by the parser.

The whole explanation can be found here:

How can i get the PHP magic constant __FILE__ work with Eclipse and PDT

The output you get is not incorrect. FILE is a special constant that gets evaluated at parser time.

Community
  • 1
  • 1
lanoxx
  • 12,249
  • 13
  • 87
  • 142
-5

As an alternative replace your __DIR__ constant with dirname(__FILE__) function

if (file_exists(dirname(__FILE__) . '/../../foo/bar')
{
    /* ... */
}
mbenegas
  • 471
  • 6
  • 16