Eli
Eli

Reputation: 4359

is it possible to treat symbolic links as actual directories when including them in PHP

I have a folder that looks like this sorta

/optik
   |- shadow
    - auth.ini
   |- htdocs
       |- index.php 

and I have an actual directory

/
  |- skel
      |- include.php

now, i made a symbolic link shadow from skel, so what ever I put in skel will appear in the shadow folder. I did this because I have dynamic files in skel that I would be changing from time to time, so i didn't want to keep having duplicate copies of the contents of skel in each of my users folders. So I thought of making a shortcut link to it.

But now I was looking to see if in my index.php i could do something like this

index.php

<?
   include "/optik/shadow/include.php";

   // content

and in my include.php

<?
   include "../auth.ini";

   // some stuff with auth.ini

My goal is that I want to know if I can use the symbolic link like an actual dir in PHP's eyes. Since from the symbolic links perspective it would need to go up 1 directory to access auth.ini, but in the actual skel directory that file doesn't exist and I would need to specify the entire directory path to my auth.ini. But since the user folders will all differ its impossible to know what directory path to specify.

How can I get something like this to work?

Upvotes: 1

Views: 6522

Answers (2)

Phil
Phil

Reputation: 164832

Given your example, to include optik/auth.ini from your optik/shadow/include.php file that is itself included from index.php, you could use the following

// optik/htdocs/index.php

// First, add the `optik` directory to the include path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(dirname(dirname(__FILE__))),
    get_include_path()
));
include 'shadow/include.php';

// optik/shadow/include.php
include 'auth.ini';

As index.php is the parent file, setting the include path from there cascades into any other included files.

Now, in regards to symbolic links, what you're proposing should work fine. What you do have to keep in mind (and this is a massive PITA) is that the __FILE__ and __DIR__ constants will resolve symbolic links. PHP offers absolutely no way to determine the unresolved path of a symbolically linked file or directory.

To illustrate, even if you include your include.php file via the shadow symlink, using __FILE__ in that file will always return /skel/include.php

See https://bugs.php.net/bug.php?id=38790

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88677

PHP should respect symlinks and follow them as if they were standard directories.

However, you are still working from the original directory in which index.php resides. This means that the include "../auth.ini"; call will be looking in /optik (the directory above htdocs) rather than the directory above that in which the included file resides.

You can use a combination of realpath() and dirname() to calculate the actual canonical paths of the files you want to get at.

Upvotes: 3

Related Questions