get current directory of symlink'd php script and not actual php script
Asked Answered
M

4

12

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)

Montserrat answered 10/5, 2012 at 0:11 Comment(1)
You might var_dump($_SERVER) to see if any of the path variables contain the path you are needing.Dira
A
14

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.

Anubis answered 10/5, 2012 at 0:46 Comment(5)
Thanks robbie for your suggestion. Using what you said I came up with this which worked for both web and cli. define('ROOT', dirname($_SERVER['SCRIPT_FILENAME']));Montserrat
Nice solution, Robbie. Could you please fix the typo in dir_name (should be dirname)Tret
Of course not - $_SERVER is added by the web server. Fortunately it's a lot easier with CLI scripts as the web server doesn't mess up the path. All you need is to read the command line that started the script: php.net/manual/en/reserved.variables.argv.php. I'll add to answer.Anubis
Readers, beware! The "$_SERVER['PHP_SELF'] or even REQUEST_URI will have it." part is entirely incorrect. They are URL paths, which have nothing to do with the actual filesystem (dir) path of the script. They may accidentally have some matching parts, but that's all. E.g. a quick test here for $_SERVER['PHP_SELF'] and $_SERVER['REQUEST_URI'] contains /prj/tag-test/prop-autotest/, while the actual dir is: /srv/szabi/prj/ALL/devtools/test/prop-autotest, as per the web server configuration...Lalia
@sz - thanks for the comment - it was add as an alternative option for those situations where the first option didn't work, and you need a fall back. I've added the word "may" and clarified it depends on server config.Anubis
H
2

If you want to get the current path with symlink you can ask your host OS:

$path = exec('pwd');

Halfmast answered 27/7, 2018 at 14:25 Comment(0)
N
1

May be useful:

var_dump($_SERVER['PWD']);
Nestling answered 5/5, 2020 at 7:42 Comment(0)
R
0

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               
Resonant answered 5/6, 2016 at 2:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.