Jump to content

How to get the site root directory?


brian914

Recommended Posts

I am running MAMP and have a number of directories with different sites in my /Applications/MAMP/htdocs directory.

 

The problem I am having is that inside one of those projects, I have a header file that gets included in pages that reside in different directories. So they need different paths for example for the style.css file. For example, from one file that path would be ../css/style.css but from another file inside another directory it would be ../../css/style.css.

 

My question is if there is a way to set a "root" path for this projects, so I can use something like $root_dir . /css/style.css and it would always work, regardless of where in the site structure I am calling it from. Something similar to what wordpress does with TEMPLATEPATH.

Link to comment
Share on other sites

Thanks a lot for the suggestion! I don't really know what I am doing... But I have created a virtual host now and I like this for a number of reasons already.

 

Is the following the correct way to refer to my style sheet? Somehow it is not quite working yet, and I wonder if it might be from the space in client name?

 

Here is how I am calling the style sheet:

<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/style.css">

 

If I look at the source, here is what it looks like which is the correct path:

(I changed the client name, but it is two works just like below)

<link rel="stylesheet" href="/Users/brian/Documents/Client Name/6_WebFiles/css/style.css">

 

Why wouldn't this pull in the style sheet?

 

Link to comment
Share on other sites

It's not permissions. You're mixing up two different but related concepts: file paths and URL paths. Quick primer:

 

File paths are paths to actual files as they are on your computer. Using Windows as the best example of their differences, C:\Windows is a file path. In PHP you use file paths whenever you deal with include()ing scripts. URL paths are in URLs. Like right now I'm at /forums/index.php on the www.phpfreaks.com website.

 

The relationship is about how the web server turns a URL into a file. "/forums/index.php" might translate into "C:\inetpub\phpfreaks\forums\index.php". You also can't confuse the two: I couldn't go to "www.phpfreaks.com/C:/inetpub/phpfreaks/forums/index.php", and in the code I couldn't

include "http://www.phpfreaks.com/forums/index.php";

 

 

With that out of the way do you understand the problem? Because the DOCUMENT_ROOT is a file path, not a URL path.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.