Get the 'URL' of an INCLUDE file

Is there a way for an include to store its own URL?
Using something like ‘PHP_SELF’,‘SCRIPT_NAME’,‘REQUEST_URI’ will return info pertaining to the file that includes the script ( of course) which is not what I want in this case.

I believe the FILE magical constant is what you are after. Though that is the files full path not a URL. If you must have a URL you could do some string manipulation to get only the part accessible via http.

oodz,

Actually FILE was my second attempt ( I should have mentioned it, but i didnt know how to elaborate on it).

What i am trying to accomplish is figure out where the ROOT of the SITE installation is. For example: the index page for the site could be directly at mysite.com or mysite.com/guests/sites/myActualsite/

so i need a way to make a url from file, even if i dont know where my initial directory actually is

I am a wee bit confused by the question because include files are normally stored outside (ie above) the site’s doc root for security reasons.

Maybe you could work out the path you need by looking at all the vars output from one of you scripts:


var_dump($_SERVER);

Perhaps explaining further about why you need to grab the url of an included file would shine a light on the exact problem you face?

No worries it is a bit confusing.

This is what I have com up with thus far, maybe it will show what i am trying to do ( actually it works!! :slight_smile: ) but maybe there is a more graceful way to do this?


	$prebase=explode("/",__FILE__);
	$pbat=isset($prebase[count($prebase)-4])? '/'.$prebase[count($prebase)-4]:'';
	$pb2='http://'.$_SERVER['SERVER_NAME'].$pbat;
        declare ('ROOT', $pb2);


<?php
	$prebase=explode("/",__FILE__);
	$pbat=isset($prebase[count($prebase)-4])? '/'.$prebase[count($prebase)-4]:'';
	$pb2='http://'.$_SERVER['SERVER_NAME'].$pbat;
        define ('ROOT', $pb2);
?>

Using define works for me.


<?php
include "dresden_test.php";

echo "This file is at ". __FILE__ ;
echo "<hr /> ";
echo "The root is at " . ROOT ;

var_dump( $prebase );

Gives:


This file is at C:\\var\\www\\html\\code\\phoenix_test.php
The root is at http://code
array
  0 => string 'C:\\var\\www\\includes\\dresden_test.php' (length=36)

EDIT, and yes http://code is the domain name for where I test stuff on localhost.

I recommend just hard coding this stuff, easier that way then trying to be clever. The application should if ever change locations (relative to the web server’s document root) and when it does it would be easy to adjust.

Btw… $_SERVER[‘DOCUMENT_ROOT’] str_replace( $_SERVER[‘DOCUMENT_ROOT’], DIR, $rel_path );

Just use

<?php
echo $_SERVER[‘SERVER_NAME’]. $_SERVER[‘PHP_SELF’];
?>

in your code that will give you the exact url of your file.