0

I've looked around for an answer and hopefully I didn't miss it or am not repeating it but here it goes. I have two files:

/var/www/index.php

/var/www/include/php/header.php

Index includes the header.php file. I want the header file to be able to traverse up two levels so I could include /include/css/index.css. Here's the problem, The header is also included by files such as

/var/www/sub/page.php

so I cannot simply use ../../ since location of the php file changes. I tried using $_SERVER['PHP_SELF'] but that changes depending on the location of the php file that includes the header file. I also tried dirname(__FILE__) but that gives me the /var/www/ part which cause complications for including.

Essentially I would like the value of $_SERVER['PHP_SELF'] from the included header.php file and not from the php script that includes it. I want the site to be modular so it can be posted anywhere without having to worry about file locations getting messed up. Is there an easier way to do this?

3
  • Are you including your CSS as an HTML include? Or combining using PHP include into the document? Because you would do a different approach... Commented Apr 10, 2013 at 17:33
  • As an HTML include. My original idea was to have those includes prepended with the root folder absolute path so that if this website was moved onto different levels it wouldn't be effected. Commented Apr 10, 2013 at 17:42
  • Check this answer to get an Url from a path stackoverflow.com/a/36101073/3626097 Commented Jun 17, 2016 at 12:35

2 Answers 2

2

Think you need use __DIR__ constant to be able include files relatively from any file. But better I think define some constant for you root file-system path, like

define( 'FS_ROOT', 'root path here' );

and when include files relatively by this constant. Also it will allow to you add constants for each used important folder and in case to change paths you will need just change some constants values

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

3 Comments

The problem with dir is that it gives you the disk absolute path and not the web absolute path which causes problems when I am trying to include my css.
For .CSS ant other URL's just add one more constant for base URL + same constants for deeper levels. And not forget about <base meta tag.
The base tag works amazing that's exactly what I wanted. Now I just need the equivalent of that for php.
1

Based on your comment, that it is an include for CSS you basically don't worry about WHERE your PHP file is, just do it from the servers perspective (web side):

<link rel="stylesheet" type="text/css" href="/include/css/index.css">

For a file in /include/css folder of your app (web exposed)

Update:

Per your updated comment, you should store a variable for your app (config), and call a function like CodeIgniter does:

$domain_url= "http://".$_SERVER['HTTP_HOST'];
$domain_url.= tr_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

function base_url($url){
    return $domain_url + $url;
}

echo base_url("include/css/index.css");

Ref: http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

4 Comments

The issues is my website is not necessarily at the web root so it could be /mywebsite/include/css/index.css and I don't want to have to program that /mywebsite/ part into it I want it to just go two levels up from where that header file is to look for includes.
Then you need to do something like CodeIgniter's base_url() where you define the root URL (domain.com/subsite) and use that fn in your code prefixing your CSS resource.
Thanks for the update. I thought about doing this I just wondered if there was an automatic way of doing it instead of having to type in the string myself.
Updated my answer, however this has constraints, CLI won't work, etc; I've seen some issues with the automated way, good for testing but for PROD I would keep it static

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.