问题
I have a script that is symlinked from one folder to another
/var/www/default/index.php
which is symlinked to
/var/www/mysite/index.php
However when i call DIR from mysite the path resolves to the origial path under default. How do i make it return the mysite path (the symlinked folder, not the actual folder)
回答1:
For Web Server requests
dirname($_SERVER['SCRIPT_FILENAME']) will give you what you need.
Failing that $_SERVER['PHP_SELF'] or even REQUEST_URI may have what you need, depending on your server configuration.
For CLI (command Line)
this won't work in cli (command line) scripts as $_SERVER is added by the web server.
Fortunately it's a lot easier with cli scripts (as there is no web server to mess about with things).
All you need is to read the command line that started the script: http://php.net/manual/en/reserved.variables.argv.php. The first argument $argv[0] is always the name that was used to run the script.
回答2:
If you want to get the current path with symlink you can ask your host OS:
$path = exec('pwd');
回答3:
If you are looking for the full unresolved system path of a cli script, SCRIPT_PATH will be insufficient.
php -f symlink/script.php
SCRIPT_FILENAME will contain symlink/script.php
I have created a php/gist that gets the unresolved path to the php file.
Here is the outcome of the function:
$ php -f subdir/mysymlink/subdir/mysymlink/subdir/mysymlink/app.php
PWD: /tmp/phpcode
SCRIPT_FILENAME: subdir/mysymlink/subdir/mysymlink/subdir/mysymlink/app.php
___FILE__ : /tmp/phpcode/app.php
getSymlink(): /tmp/phpcode/subdir/mysymlink/subdir/mysymlink/subdir/mysymlink
来源:https://stackoverflow.com/questions/10525880/get-current-directory-of-symlinkd-php-script-and-not-actual-php-script