3

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.

4 Answers 4

4

Why are you even trying to use a relative path? Can't you just link it as /css/main.css? Then it will always be relative to the root of your site, rather than the current directory.

Sign up to request clarification or add additional context in comments.

8 Comments

It's possible this would work on the final site, but for both local testing purposes, and the server I am hosting on, the site root isn't right after the .com
+1 That's why it's always a good idea to use the same ROOT in your development and production environments.
@user730492 why aren't you use virtualhosts and fake sub local-domains? Like "website1.local" -> add this to your hosts file and make it point to 127.0.0.1 (or the IP of the dev server) and then configure a vhost in your local http server. In fact you could even use the real domain and make a whole subdomain point to 127.0.0.1, like local.yourdomain.com.
Thanks Capsule. Right now I'm using easyPHP and I have an alias which lets me connect from (127.0.0.1:8888/site), I'm not sure about what else you may be talking about but I'll try to look into it.
@capsule yeah I'm not really sure how to do all that, if there's a resource you know of that would help me, that'd be awesome.
|
3

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:

echo '<link rel="stylesheet" type="text/css" href="'.$BASE.'/css/main.css" />'

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:

$BASE = __DIR__;

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.

6 Comments

Not a bad idea, although I don't like the idea of having to change the $BASE everytime I want to do local testing.
Because you don't have a proper testing environment ;-) See my comments on @Mark's answer.
You can have the BASE defined automatically using the __ FILE __ construct in the config file :)
How (more specifically) would I do that? And as in the php install config files?
@Capsule: I use a similar pattern, though I do have proper testing and production environments. However, I don't have a staging environment and I need to resort to shared hosting, so I am putting website in a directory... and then I need this mechanism. If I were able to use subdomains, absolute paths would be perfect.
|
1

hm.. I would recommend to link the css absolutely…

otherwise you could

echo str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');

3 Comments

Oh wow this actually worked! I had to make it str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/') - 2); though, just because i'm not on the very root of the URL at the moment. But thanks man!
Sort of answers the question, but it's a bit of a wasteful way of doing it. @Palantir's answer below makes more sense, and is a more common pattern.
@LucasUP Just read it. The best way is to use Palantir's method. I have this same problem. My local dev server has just one host for all the projects I work on, I have different staging servers depending on the client I'm working on and then typically one domain for each project. A constant for the current path can be used for all sorts of things, including your problem here, which is the advantage over the 'one-use' solution by @Flask.
0

you must get your site base directory using $_SERVER['DOCUMENT_ROOT']. Then put this value in session. Then you can use in base index.php

$dir = $_SERVER['DOCUMENT_ROOT'];
$include = $dir. "/includes/functions.php";
$_SESSION['include_dir'] = $include;

Other files you can use

reqire($_SESSION['include_dir']);

2 Comments

This one's a bit above me, but I think I understand the gist. Haven't actually done any session stuff yet.
if you can get you base path and your site path will not change, then you can define it as static variable in your config file, then use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.