Okay I’ve been trying to figure this out for a while now.
I have a website I’m working on and here is basically my structure:
index.php
page-x/
index.php
page-x-y/
index.php
include/
functions.php
css/
main.css
Except add on more sub-folders and index pages.
Basically pages at three different levels.
They all need to have css/main.css, but the path from any one index page is going to be different.
Right now I use functions.php to add the CSS, and already have to manually enter the relative path from an index page to functions.php (ie require_once("../include/functions.php"); or ../../include etc
How can I make it so that functions.php can figure out the relative path from any index page it is included on, to css/main.css?
I.E. how would I define $relDir for the following code in functions.php:
echo '<link rel="stylesheet" type="text/css" href="' . $relDir . 'css/main.css" />';
I could manually pass "" or "../" or "../../" every-time to the function, but can I avoid that?
EDIT 3
If I define $BASE = dirname(__DIR__); in functions.php, I can find the root of the site and go from there to get the absolute path to the CSS.
However the css doesn’t seem to link properly, on my local machine the resulting link looks like: (I can manipulate the slashes to be consistent but it doesn’t help)
"C:\Documents and Settings\Lucas\site/css/main.css"
(Note: I am using an alias on easyPHP so that I can connect to this address from http://127.0.0.1:8888/site/ but__DIR__ finds the ACTUAL directory of the file.
And on the (My universities) server I am testing on comes back with
"/Volumes/Web/Students/MYNAME/Sites/MYSITE/css/main.css"
The ACTUAL URL of the website looks like:
http://www.SCHOOL.com/MYNAME/MYSITE/
The CSS wont link in either situation
EDIT 2 I can define $relDir as:
str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');
returns one of the following depending on how deep the index is:
""
"../"
"../../"
This a lot of processing apparently though. It sounds like the best thing to do would be to look into proper testing environments, and use an absolute path. I’ll look into that. I’m still a bit of a newb with some of that stuff.
EDIT 1: Okay most people are suggesting I stick with just doing it manually, originally had a function call something like this:
<?php require_once("../include/functions.php");
buildHead("Music", "../"); //($title, $relDir) ?>
I just have to change the ../ to whatever in both places for every page.
Easiest way? Use absolute paths. If your website just stays on a single fixed path it’s easy. If you want to keep yourself free to change the structure of your site, add a configuration parameter — eg. include a config file on top of each PHP file and define a $BASE parameter there which holds the absolute path of your app base, then do:
If $BASE is empty, it will default to the root of your webserver.
You can also do all this processing automatically, using the __DIR__ magic constant (or __FILE__ if you are using PHP < 5.3 but it doesn’t change much – you just need to strip the file name from what you get). If you include this config file at the top of each PHP file (or you use apache auto prepend directive), and your config file is located in the base directory of your website, then this will be sufficient:
Otherwise, if it sits in a directory, you just need to go up, but this is known when you are deploying and is not dependent on the request.