Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4558450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:39:06+00:00 2026-05-21T17:39:06+00:00

Okay I’ve been trying to figure this out for a while now. I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:39:07+00:00Added an answer on May 21, 2026 at 5:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have been racking my brain trying to build a JSON array from
Okay so im working on this php image upload system but for some reason
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, this seems like it should be relatively simple, but I've been Googling for
okay so i'm working with visual studio 2010. I need to read out the
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay, so I'm making a table right now for Box Items. Now, a Box
Okay. I know this looks like the typical Why didn't he just Google it
Okay say I have two timestamps saved in database in the format yyyymmdd, a
Okay, so I've been running a Java/Jersey webservice on Tomcat with basic authentication which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.