Forum Moderators: coopster

Message Too Old, No Replies

$ SERVER['DOCUMENT ROOT'] not giving expected dir root

         

mr_nabo

11:25 pm on Oct 31, 2008 (gmt 0)

10+ Year Member



I'm using an installation of MAMP on my Mac to develop locally, but everytime I put in the code:

$_SERVER['DOCUMENT_ROOT']

it returns this:

/Applications/MAMP/htdocs/projectname/templates/...

instead of what I expected, which is this:

/projectname/templates/...

It means that everytime I want to reference to the root of the site, I can't because it seems to link to the Applications directory.

Any ideas what I'm doing wrong?

Thanks

kazisdaman3

6:09 am on Nov 1, 2008 (gmt 0)

10+ Year Member



You may just want to hardcode the DOCUMENT_ROOT yourself then in this case. and reset the varaible

mr_nabo

11:01 am on Nov 1, 2008 (gmt 0)

10+ Year Member



Thanks for that, seems to be a good solution

jatar_k

12:19 pm on Nov 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would suggest using a separate constant as opposed to resetting a server var

if the DOCUMENT_ROOT is returning something different than you expect you might want to look at the apache conf to see what the root really is set to

mr_nabo

10:59 pm on Nov 2, 2008 (gmt 0)

10+ Year Member



Hi jatar_k,

Something that is strange, is that when I do this:

<a href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>">Homepage</a>

The link turns out this:

<a href="http://www.domain.com/home/username/public_html">Homepage</a>

But when I do this:

<a href="<?php $_SERVER['DOCUMENT_ROOT'] ?>">Homepage</a>

or

<?php echo '<a href="' . $SERVER['DOCUMENT_ROOT'] . '">Homepage</a>'; ?>

The link turns out this:

<a href="http://www.domain.com/">Homepage</a>

I've tried this on my local dev setup and two separate webhosts and it seems to be the same on all of them.

Strange. My apache conf has the root setup similar to the first link (i.e. /Applications/MAMP/htdocs).

Any ideas why it's like that? I suppose I could always just use the second method, but sometimes that's really inconvenient...

jatar_k

12:19 am on Nov 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yep I can tell you

<a href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>">Homepage</a>
<a href="http://www.domain.com/home/username/public_html">Homepage</a>

the above is correct, the domain is being prepended, basename or something. I am guessing the domain is being spit out and '/home/username/public_html' is the actual path.

<a href="<?php $_SERVER['DOCUMENT_ROOT'] ?>">Homepage</a>

is not working because the syntax is wrong, you're not echoing anything so it isn't outputting the path. All you are getting is the prepending we noticed before.

<?php echo '<a href="' . $SERVER['DOCUMENT_ROOT'] . '">Homepage</a>'; ?>

also a syntax error $SERVER is not correct obviously, so it isn't set and echoes nothing.

I am not 100% sure what is causing the prepend but I am sure that can be easily figured

mr_nabo

12:48 am on Nov 3, 2008 (gmt 0)

10+ Year Member



Sorry, it's been a long night - I see those errors.

Let me explain what I'm trying to do: I'm configuring a template for Joomla and need to reference an images folder in a *template* directory (root/templates/templatename/images/home/) so that I can create a list of images in my HTML. My code is as follows (thanks to eelixduppy for this):


<?php
// Set variables
$dir = $this->baseurl . '/templates/templatename/images/home/';
$good_ext = array('.jpg','.gif','.png');

if ($handle = opendir($dir)) {

while (false!== ($file = readdir($handle))) {

$ext = strrchr($file,".");
echo $ext . '<br />';

if(in_array($ext,$good_ext)) {
//do something with file

echo '<img src="' . $file . '" /><br />';

} else {

echo 'BAD IMAGE...BAD: ' . $file . '<br />';

}

} closedir($handle);

} else {

echo 'Directory does not exist!<br />';
echo $dir;
}

?>

In order to link to the root of the site, Joomla has the following variable defined: '$this->baseurl'. Much like with $_SERVER['DOCUMENT_ROOT'], I can't use '$this->baseurl' within a php script.

For example, this doesn't work when I want to use the variable $dir in the script above:

<?php $dir = $this->baseurl . '/templates/vitalarts/images/home/'; ... ?>

But I can create a link to the homepage or any other folder within the site by doing this:

<p>This is the root: <a href="<?php echo $this->baseurl; ?>" />Homepage</a></p>

So how do I reference the images folder in my script correctly? I'm assuming the problem is because Joomla doesn't work on a directory structure for its pages and I'm working on a script in a subdirectory - Joomla seems to call in various php pages into one index.php from what I can see, whereas my template php page is in root/templates/templatename/images/home as I said above.

Thanks for all your help so far.

mr_nabo

1:33 am on Nov 3, 2008 (gmt 0)

10+ Year Member



Just to add to the confusion, I thought I'd say that adding this after the script works i.e. it displays an image from the folder:

<?php echo '<img src="' . $dir . 'image_name.png" />'; ?>

So confused!

jatar_k

3:50 pm on Nov 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



from what you've mentioned you should be able to echo the doc root

just do a straight echo of

echo $_SERVER['DOCUMENT_ROOT'];

and see what it gives you