Page 1 of 1

Having trouble with relative paths...

Posted: Thu Jan 08, 2009 7:25 am
by benburgess
Hey folks.

I'm having some trouble with the following snippet. It works fine on my machine but on some machines it doesn't work.

I've pinned the issue. On some machines the "__FILE__" output includes a ".peep" directory in the path that isn't in the "DOCUMENT_ROOT" readout.

Code: Select all

echo dirname(__FILE__);
 
output: 
/home/.peep/user/domain.com/test/

Code: Select all

echo $_SERVER['DOCUMENT_ROOT'];
 
output: 
/home/user/domain.com


This is the code:

Code: Select all

$sq_this_site = substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT']));
$sq_this_dir = dirname(str_replace('\\', '/', $sq_this_site));
 
$url_protoc = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
 
$sq_this_sq = $url_protoc.$_SERVER['HTTP_HOST'].'/'.$sq_this_dir;
 
$sq_this_site = $sq_this_sq.'/?uk='.$sq_unique_key;
$sq_initial_query = $sq_domain.'?cb='.urlencode($sq_this_site);
 
?>
<link rel="stylesheet" href="<?php echo $sq_this_sq; ?>/cssdocument.css" type="text/css" />
<script src="<?php echo $sq_this_sq; ?>/script1.js" type="text/javascript"></script>
<script src="<?php echo $sq_this_sq; ?>/script2.js" type="text/javascript"></script>
<script src="<?php echo $sq_initial_query; ?>" type="text/javascript"></script>

Re: Having trouble with relative paths...

Posted: Thu Jan 08, 2009 8:59 am
by jaoudestudios
$_SERVER['DOCUMENT_ROOT'];
Return the location of the script, on different servers it will be in different places depending on the server setup.

Re: Having trouble with relative paths...

Posted: Thu Jan 08, 2009 10:54 am
by benburgess
I'm looking for a way to get the web directory of the currently executing file. $_SERVER['
DOCUMENT_ROOT']; always results with the file that is including the above file. I want the returned root to be that of the file above no matter where it is being included.

Also, web root not document root because this is for <Script> tags and other such references that are in the HTML layer.

Re: Having trouble with relative paths...

Posted: Thu Jan 08, 2009 10:59 am
by benburgess
jaoudestudios wrote:
$_SERVER['DOCUMENT_ROOT'];
Return the location of the script, on different servers it will be in different places depending on the server setup.
Would return /home/user/domain.com/test/.... a file path containing directories outside of the web root!

I need to convert the __FILE__ readout to http://domain.com + /test/