2

Let's pretend I have a symbolic link pointing to /some/fake/location from /some/real/location

In that hypothetical directory I have a file named blah.php which reads:

#!/usr/bin/php    
<?php echo __dir__; ?>

I then execute blah.php from the command line like so:

php /some/fake/location/blah.php

The output is

/some/real/location

How do I get the current PHP file's directory without resolving any symbolic links?

LogicalException
  • 566
  • 2
  • 7
  • 16

2 Answers2

3

After googling everywhere it seems that a million others need __dir__ not to resolve symbolic links. Unfortunately, however, the answer is that there is no answer.

LogicalException
  • 566
  • 2
  • 7
  • 16
2

Since __DIR__ resolves symlinks automatically, you may want use $_SERVER["SCRIPT_FILENAME"] instead. It will returns the absolute pathname of the currently executing script.

Federkun
  • 36,084
  • 8
  • 78
  • 90
  • Not quite what I want but I have already tried messing with that variable. `blah.php` includes other files and I want those files to know what directory they're in. `$_SERVER['SCRIPT_FILENAME']` only returns the full path of `blah.php`. – LogicalException Apr 06 '16 at 22:42
  • 1
    Does answer here help: http://stackoverflow.com/questions/12580330/php-dirname-returns-symlink-path ? – ficuscr Apr 06 '16 at 22:44
  • Looks like the exact same question but with a completely different accepted answer. `blah.php` has no idea if it's in a symbolic directory and is also clueless has to where it's stored. `readlink(__dir__)` would also return the real directory instead of the wanted fake one. – LogicalException Apr 06 '16 at 22:54