__FILE__ and sym-links

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a problem I can't crack.

I have the following example directory structure
/some_arbitrary_path/htdocs/    ( also $_SERVER['DOCUMENT_ROOT'] )
/some_arbitrary_path/htdocs/common is a symbolic link to

if $_SERVER['DOCUMENT_ROOT'].'/common/some_script.php' gets
it works just fine, however, within some_script.php
__FILE__ == /some_different_path/common/some_script.php

How do I determine that __FILE__ does in fact fall inside the
and/or, get a the value of the path that I included (one with /
and/or, determine the "html path" to some_script.php is '/common/

Re: __FILE__ and sym-links


Quoted text here. Click to load it

How the user 'got' there is usually easily checked by examing the $_SERV=
ER  =

array (PHP_SELF/REQUEST_URI), this only tells you about the 'entrypoint'=

though, not the location of the file ro the root if it was included.

To the real question:"Can I determine how this file was included?": Not =

easily AFAIK. You could play around with __FILE__, get_included_files(),=

is_link() && readlink(). What are you going to use this information for =

BTW? You might be better off checking getcwd() then __FILE__...

A quick mock up:
function _euhm_included_by_name_or_something($file){
    $included = get_included_files();
    //straight include
    if(array_search($file,$included)!=3D=3Dfalse) return $file;
    foreach($included as $include){
        //included as symlink?
        if(is_link($include) && readlink($include)=3D=3D$file) return $include=
    return false;

With a file included multiple times using different symlinks, one cannot=

determine by which one it was AFAIK. And offcourse, one might have to wa=
lk  =

through the path do determine wether one of the directories was a symlin=
k,  =

absolute paths differ from relative ones, etc, etc (haven't got a linux =

box to test properly handy right now) but you get the idea.

A really, really dirty way of determining how this file was called, is  =

offcourse to perform a debug_backtrace() as the first statement in the  =

file, and check with what arguments a possible include_*/require_* was  =

actually called. Unless absolutely needed, I'd stay clear of this option=
:  =

debug_backtrace() should be for debugging, and can create a considerable=

unwanted overhead.
-- =

Rik Wasmus

Re: __FILE__ and sym-links

Thanks for your help..
I figured out how to accomplish this.. in a nutshell:

I explode('/',__FILE__);
I then loop backwards through the array prepending a "relative path"
with the current explosion value.. until
realpath($_SERVER['DOCUMENT_ROOT'].'/'.$rel_path) == __FILE__

Site Timeline