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'] ) /...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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'? |
|
|||
|
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 |
|
|||
|
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__ |