__FILE__ and sym-links

This is a discussion on __FILE__ and sym-links within the PHP Language forums, part of the PHP Programming Forums category; I have a problem I can't crack. I have the following example directory structure /some_arbitrary_path/htdocs/ ( also $_SERVER['DOCUMENT_ROOT'] ) /...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-21-2007
BKDotCom
 
Posts: n/a
Default __FILE__ and sym-links

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
/some_different_path/common/

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

So:
How do I determine that __FILE__ does in fact fall inside the
DOCUMENT_ROOT tree?
and/or, get a the value of the path that I included (one with /
arbitrary_path)
and/or, determine the "html path" to some_script.php is '/common/
some_script.php'?
Reply With Quote
  #2 (permalink)  
Old 11-22-2007
Rik Wasmus
 
Posts: n/a
Default Re: __FILE__ and sym-links

On Wed, 21 Nov 2007 23:56:50 +0100, BKDotCom <bkfake-google@yahoo.com>
wrote:

> 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
> /some_different_path/common/
>
> if $_SERVER['DOCUMENT_ROOT'].'/common/some_script.php' gets
> included...
> it works just fine, however, within some_script.php
> __FILE__ == /some_different_path/common/some_script.php
>
> So:
> How do I determine that __FILE__ does in fact fall inside the
> DOCUMENT_ROOT tree?
> and/or, get a the value of the path that I included (one with /
> arbitrary_path)
> and/or, determine the "html path" to some_script.php is '/common/
> some_script.php'?


How the user 'got' there is usually easily checked by examing the $_SERVER
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)!==false) return $file;
foreach($included as $include){
//included as symlink?
if(is_link($include) && readlink($include)==$file) return $include;
}
return false;
}
//usage:
_euhm_included_by_name_or_something(__FILE__);

With a file included multiple times using different symlinks, one cannot
determine by which one it was AFAIK. And offcourse, one might have to walk
through the path do determine wether one of the directories was a symlink,
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
Reply With Quote
  #3 (permalink)  
Old 11-26-2007
BKDotCom
 
Posts: n/a
Default 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__
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:01 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0