View Full Version : Resolved Always make paths relative to root
04-10-2009, 10:51 PM
My site is split up in includes. In one include a link to the css file is placed. The images in the css file are relative to my root like this:
However, when the CSS file is included in another page deeper in the hierarchy, the image path in the CSS file does not fit anymore thus the image doesn't display.
Isn't there a way to define all paths relative to the root so that it will work no matter where the CSS is linked from??
04-10-2009, 10:55 PM
Hmm, I might be wrong, but I always thought that CSS-called images were called up based on the path in the CSS (in other words, in a path relative to where the CSS file itself is located, regardless of where the HTML or PHP page is located).
Am I wrong about this or is there a different way that it works for PHP? I'm not very PHP savvy just yet so I'm curious...
04-10-2009, 11:00 PM
* deleted - my mistake *
04-10-2009, 11:05 PM
That is strange. Also, I didn't read your post carefully enough to notice the question at the bottom (duh!)
And the answer is this:
You just drop the ".." from the front of the string.
04-10-2009, 11:07 PM
No wait, I am mistaken, it is like you say! Sorry for that!
Hold on - trying
04-10-2009, 11:16 PM
That's how it goes when you're really not aware what you're asking.... (I might have been sitting here for too long).
The question really does not have anything to do with my css file (doh).
This is the case:
In my page I have a link to an image:
<img src="images/image.png" alt="image" />
It works good when the page is located in the root. However, when the page is moved further down:
Root --> Folder1 --> Page.php
the image does not show. Of course. But, when the link is changed to this (slash added):
nothing shows at all - even not at the page in the root where it showed before. That's problematic as the link is included in every page and therefore has to be relative to it all, if you know what I mean. How would I come around that?
04-10-2009, 11:20 PM
nothing shows at all - even not at the page in the root where it showed before.Well that simply shouldn't be. Do you have a link to your page so I can take a look and make sure there are no other issues at play?
04-10-2009, 11:35 PM
04-11-2009, 12:05 AM
I fixed a couple of errors relating to my site being in an actual test map and NOT in the server root - as result this will be my last post tonight.
Now, with that fixed, all images show regarding of the location of the file. It worked with the leading slash as you said!
However, none of my actual includes ARE getting included when their paths have a leading slash -
link(page in root): http://behrentzs.com/1.php
link(page outside root): http://behrentzs.com/svmanager/
Code example (does not load into page):
<?php include("/includes/header.php"); ?>
Code example (does load into page):
<?php include("includes/header.php"); ?>
Considering the page is in the root of the server. None of the above loads includes into pages placed outside the root.
04-11-2009, 12:16 AM
With PHP you are out of my area of "expertise" (if I can call myself an expert in anything web-related).
I'm not sure if there is anything syntactically wrong with using the leading slash for filepaths on PHP includes or not. I can't see any reason why there would be any problem using that, but I am such a PHP novice that my judgment on this should be taken with a grain of salt.
If you don't get any better-informed responses to your question here you may want to ask a moderator to move this thread over to the PHP section or else re-post there (since the start of this thread wasn't really PHP-related to begin with).
Before any of that though, have you searched google or the archives here for this yet? There might be an explanation out there already.
04-11-2009, 10:10 AM
<?php include("/includes/header.php"); ?> When dealing with PHP, the absolute path starting with / may not be like the path in the client side(browser).
From the serverside, / stands for the document_root, which can be identified by echoing $_SERVER['DOCUMENT_ROOT']. Thus if your includes directory is not directly under your document_root, you may define a named constant (http://php.net/define) at the top of your file( say inside a config.php file, if exists), like
define(_ROOT_, $_SERVER['DOCUMENT_ROOT']."/directory/subdirectory"); and then easily include your PHP files like
04-11-2009, 02:14 PM
Upon echoing the server root I get this:
This is what I inserted between my <head> and </head>:
//Config file - configures the document root and thus the file path used in includes
This is what I have in my PHP:
<?php include(_ROOT_."/includes/header.php"); ?>
But with no result unfortunately.
04-11-2009, 06:05 PM
This worked with the includes:
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); ?>