Forum Moderators: coopster
$_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
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...
<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
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 fileecho '<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.