11

As far as I am aware, php's getcwd() (and similar functions eg dirname(__FILE__)) are supposed to return the current directory of the file being executed.

If the current directory happens to be a symlink to another directory, php (presumably in conjunction with apache) is supposed to return the path showing the symlink name as the 'directory' you're in.

Example:

from /var/www,
directory 'one' contains index.php
symlink 'two' points at directory 'one'

one/index.php: <?php echo getcwd(); ?>

Accessing http://localhost/two/index.php in a browser shows /var/www/one.

I would expect it to show /var/www/two

Does anyone know if this is a php or apache setting I can change? Or am I unable to use symlinks in this manner?

cwallenpoole
  • 72,280
  • 22
  • 119
  • 159
myk00
  • 299
  • 1
  • 3
  • 8
  • 1
    Did you figure it out? I'm interested in this same use case. – kalenjordan Mar 30 '12 at 00:08
  • Is Apache configured to follow symLinks? Like `Options FollowSymLinks` – Jérôme Mahuet Mar 30 '12 at 01:09
  • I don't think this has anything to do with Apache. It's a PHP "feature". If you enter this at the command line `mkdir a; ln -s a b; php -r 'chdir( "b" ); echo getcwd();'`, php will show you that you're in a directory called 'a', even though you chdir()ed to 'b'. – edam Oct 22 '13 at 13:47

1 Answers1

11

OK figured it out. Use $_SERVER['SCRIPT_FILENAME'] if over the browser. Over the command line, you can get the current symlinked directory using exec("pwd")

kalenjordan
  • 2,346
  • 1
  • 19
  • 38