...

View Full Version : Relative paths



Nightchild
11-16-2007, 01:56 AM
Running into problems with relative paths for various files. I'm trying to segregate related scripts into their own folders. ie header & footer into a template folder. admin functions into an admin folder etc...

I'm running into the most basic problems such as - a page in the admin folder includes the header file.

include("../template/header.inc.php");

The header file is being included ok but the CSS link inside the header file isn't correct.

echo ' <link rel="stylesheet" type="text/css" href="' .$APP_BASE. '/template/screen.css" media="screen" />';

$APP_BASE is created using

$APP_BASE = '//' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

The generated CSS link turns out as:

<link rel="stylesheet" type="text/css" href="//nexus/raw/admin/template/screen.css" media="screen" />

How do I create these links properly? I can't use "../template/screen.css" because I have a few general files which are in the application root. Using the "../" causes the file path to be in the server root in that case.

Appreciate the help.

CFMaBiSmAd
11-16-2007, 05:28 AM
... the CSS link inside the header file isn't correct.You did not actually state what was wrong with that CSS link (other than it should have a http: before the // to make it a properly formed, fully specified, URL.)

But, I will guess that because dirname($_SERVER['PHP_SELF']) is the path to the file that is including the header and that this changes depending on which file is including the header file, that you are getting a varying path to the css file?

Don't use the $_SERVER['PHP_SELF'] in this situation. If you are trying to make an absolute URL to the css file, just do that -

$APP_BASE = 'http://' . $_SERVER['HTTP_HOST'];
...
echo '<link rel="stylesheet" type="text/css" href="' .$APP_BASE. '/your_actual_full_path_here_to_the/template/screen.css" media="screen" />';

Also, because files that the browser fetches directly (images, external javascript, external css...) use the / = "domain root" rule, you can also just use -

echo '<link rel="stylesheet" type="text/css" href="/your_actual_full_path_here_to_the/template/screen.css" media="screen" />';Edit: Also, take a look at the __FILE__ magic constant - http://www.php.net/manual/en/language.constants.predefined.php if you want to get the path to the included file instead of the file that is doing the including.

johnnyb
11-16-2007, 06:56 AM
I think you have 2 options:

1) Add more to the href that calls the CSS file. I was going to say to use $_SERVER['HTTP_HOST'] - but you are already. However, it doesn't appear to be creating any output. You'll need to figure out why - I have no idea at this late hour ;)

2) don't bother putting the domain name - just use a directory path starting with "/" So, if your CSS file is at http://www.example.com/templates/style.css then your link tag should have href="/templates/style.css" and then you may be able to leave PHP right out of it, (this is what I usually do).

aedrin
11-16-2007, 04:17 PM
Or just use the base tag.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum