I beg my pardon Scallio, you were right.
You've got the problem right, even in spite of the OP said that it were hosts to differ, not folders.
| SitePoint Sponsor |





I beg my pardon Scallio, you were right.
You've got the problem right, even in spite of the OP said that it were hosts to differ, not folders.
With dirname(__FILE__), doesn't it get the location of the current file? So if this header.php is included into several sublevels, the directory mapping will be inconsistent. Rather, if you use the while-loop, you create your own root with a root.txt (yes, the manual part) and then work from there.
As for the second "bliss", I see your point. I think my main problem is understanding the differences between an application and resources. I considered all PHP files (application) and images and CSS files (resources) to be equals. They are not, it seems?
So while the / will work with CSS, it will not work with PHP. Is this correct?

dirname(__FILE__) will give you the path of the file that calls that statement, so if your header.php is in /home/scallio/ and you include it in /home/scallio/public_html/some/terribily/long/path/index.php, it will still give you /home/scallio
The files that include header.php might have different locations, but header PHP has one location, and putting dirname(__FILE__) in header.php will give you that location. __FILE__ refers to the file that it is in, not to the file that is called by the browser.
Correct. / on your website is your document root, / in PHP is the file system root.
I'll test this later tonight, but thank you very much for the clarity so far. I didn't understand the difference between the two before.
How does CSS know what my document root is? Would it be "public_html", or "website" (which is 1 sublevel beneath public_html)?





Please, do us a favor: can you please provide a code to perform highlighted action?so if your header.php is in /home/scallio/ and you include it in /home/scallio/public_html/some/terribily/long/path/index.php,
Thank you

Alright, let me give you all an elobarate example on how I used to set up websites (before I started using Yii) to hopefully clear up this problem once and for all.
Suppose you are building a website that a home page, an "about us" page and a contact page.
You would need 3 files, home.php, about-us.php, and contact.php.
Typically these files go into a directory that is not accessible by web browsers and get included by index.php that is accessible by the browser.
You will get a file setup that roughly looks like this:
/home/scallio/config.php
/home/scallio/includes/home.php
/home/scallio/includes/about-us.php
/home/scallio/includes/contact.php
/home/scallio/includes/header.php
/home/scallio/includes/footer.php
/home/scallio/public_html/index.php
Now, since index.php is the only file in the public_html directory, it's the only one that the web brower can access. The browser doesn't know and doesn't need to know about all the other website.
Furthermore, suppose you want to run this website on different hosts. On the one host the file layout is as I specified above, but on the other hosts all files live in /var/www/scallio.
Now in the config, you want to know in which path it resides, because this could either be /home/scallio, or /var/www/scallio, so the config file would look something like this
Now for the index.php you need to include the config.php, which is one directory up, soPHP Code:<?php
$root = dirname(__FILE__);
// more config here for databases etc
Now we have a variable called $root that gives us the root path for the website, for the first host it is /home/scallio and for the other host it is /var/www/scallio, which is exactly what we want.PHP Code:<?php
include('../config.php');
Now to include the header.php and the footer.php in the index.php, we can extend index.php as follows:
That will tell PHP to include /home/scallio/includes/header.php and /home/scallio/includes/footer.php on the one host, and /var/www/scallio/includes/header.php and /var/www/scallio/includes/header.php on the other host.PHP Code:<?php
include('../config.php');
include($root.'/includes/header.php');
include($root.'/includes/footer.php');
Now, to include the different pages, let's say we give the index.php a GET parameter called page, that indicates which page to include, but if no page is given, we want to include home.php
That would mean we need to alter the index.php to become
[PHP]
So, now we can access /index.php, /index.php?page=home, /index.php?page=about-us, /index.php?page=contactPHP Code:<?php
if (isset($_GET['page'])) $page=$_GET['page']; else $page='home';
$page = ltrim($page, '/.');
if (file_exists($root.'/includes/'.$page.'.php')) {
$file = $root.'/includes/'.$page.'.php';
} else {
header('HTTP/1.0 404 Not Found');
}
include('../config.php');
include($root.'/includes/header.php');
if (isset($file)) {
include($file);
} else {
echo ('404 - The page you were looking for could not be found');
}
include($root.'/includes/footer.php');
Now suppose we don't have the website in the root of the TLD, but in some folder called mywebsite. Thus we get a file layout like
/home/scallio/config.php
/home/scallio/includes/home.php
/home/scallio/includes/about-us.php
/home/scallio/includes/contact.php
/home/scallio/includes/header.php
/home/scallio/includes/footer.php
/home/scallio/public_html/mywebsite/index.php
The only thing we need to change to take this into account is the include of the config.php file in the index.php
Namely, we now need to do
Because the config.php is now two levels up, instead of one. Note that $root has not changed, since config.php is still in /home/scallio and dirname(__FILE__) will still return that path.PHP Code:<?php
include('../../config.php');
After this change your website will run happily in the /mywebsite subdirectory.
Try it if you don't believe me, I promise you it works.
So now that we have set up all PHP paths correctly, let us worry about the images and CSS files.
Suppose we add a main.css and a logo.jpg to our file layout as follows (assuming the website is running in the root of the TLD again, and not in /mywebsite):
/home/scallio/config.php
/home/scallio/includes/home.php
/home/scallio/includes/about-us.php
/home/scallio/includes/contact.php
/home/scallio/includes/header.php
/home/scallio/includes/footer.php
/home/scallio/public_html/index.php
/home/scallio/public_html/css/main.css
/home/scallio/public_html/images/logo.jpg
Now, if want to include the css file and the image in the header.php, I would get
Note that the paths I indicate here start at /, where / is equal to /home/scallio/public_html on the file system, but the browser doesn't care about that!Code:<link rel="stylesheet" type="text/css" href="/css/main.css" /> <img src="/images/logo.jpg" alt="My Company Logo" />
Now, if I add the directory mywebsite again and leave the image and css file where they are, the links still work!





well, you're talking of the relative path and you just don't have a solution for the absolute one
quod erat demonstrandum





I spent whole page trying to explain it to hash
Then you came in, and said:
But you failed to provide a code that utilizes this language construct in this topic's context.Using dirname(__FILE__) is much nicer in my opinion.
Okay, I'll try once more.
I hope we all going to agree that use of absolute path is very handy.
Because one can use the same piece of code, no matter where it called.
For the virtual server it's just full path from the virtual server root.
As I can see, we all share same opinion: using absolute path for the virtual server is very good.
Example:
Say we have 2 pages,
/templates/users/short.php and
/super/long/path/from/virtual/root/long.php
also we have a picture,
/image/pic.jpg
and if we use absolute path to address this picture, like this:
<img src="/image/pic.jpg">
it work like a charm in both long.php and short.php
because the path is absolute.
And we all agree it is good. (I hope)
Now we move to the filesystem. Here is our apple of discord.
I assume, one may want to have the same "master key", same magic wand as we have in the virtual server. Same absolute path thing, which give us one piece of code to be used everywhere. No matter where it called and where it moved afterwards.
The thing the OP, I hope, were asking for.
Same example
Same old 2 pages,
/templates/users/short.php and
/super/long/path/from/virtual/root/long.php
but now we want to include a file, say
/includes/page.php
Here is the problem:
What code we have to use in both long.php and short.php to include /includes/page.php?
How can we utilize absolute path thing in this case?
Is there a universal code to include your global config.php?
Is this explanation exact enough?
So above, I recently got how CSS and PHP look differently into the directories (browser vs file system).
Isn't that line of code, <img src="/image/pic.jpg"> inside of a PHP file? Therefore, wouldn't it look deeper into the file system?and if we use absolute path to address this picture, like this:
<img src="/image/pic.jpg">
it work like a charm in both long.php and short.php
Or: is it only with PHP includes that you have to worry about the file system root depth? Whereas with <link type=".." rel=".." href=".." /> or <script type=".." />, it is the browser looking, and the simply / will work. And the same, I assume works while you're in the .css files, that you only need to worry about /.
So again, to reiterate, the only problem of finding the root comes in with PHP-includes, not with anything else?





PHP works on the server side.
the result of this work is HTML text
HTML text being sent to the browser.
as if it was HTML file on the server.
when browser read this HTML,
it find <img src="/image/pic.jpg">
then it make another request to the server
to get /image/pic.jpg
so, this line yes, in the php file.
but it become part of HTML sent to the browser.
clear pure HTML with no sign of PHP
as if it was just HTML file





lol, what did you explain?
I think you are trying to say the web root is absolute for php? How so? The php can live anywhere, so you either have to know where it is in relation to the doc root, or where it is in relation to where your entry point is. It's all relative.
FACT: It's easier to type include $root.'includes/file.php' than include $_SERVER['DOCUMENT_ROOT'].'../includes/file.php'
and also easier to read.
![]()
I have finally resolved my problem! Success!
I want to greatly thank everyone for all the support on this problem.





but you have to define $root first.FACT: It's easier to type include $root.'includes/file.php' than include $_SERVER['DOCUMENT_ROOT'].'../includes/file.php'
and also easier to read.
Bookmarks