Direct Path Names for Hosting Using PHP

yeah it’s getting late

off to bed, but still curious: dirname returns the file sys name, so you can make an absolute file path from anywhere … I don’t see the problem, or need for doc root.

with dirname you’re finding absolute path for the parent script.
but for the /includes/file.php you’re still using relative. you presume that /includes/file.php is in the same directory. but it may not

I am talking of absolute path for the /includes/file.php, with which it can be called from anywhere, like we do it for the CSS

dirname(FILE)

try it, it will tell you the path to the current file, no matter how or where it was included, or who the parent is.

the goal is to set $root, whatever it is. then $root./whatever/the/hell is easy

i did it 1000 times.
good trick to pick a file in the same directory, no matter what current directory is
but it has nothing common with root thing and absolute path

oh no. correct myself.
it will give you absolute path, yes. to your parent script.
but it wont help you to include any other file, unless you know it’s relative path from your parent script

I thought I’ve found right explanation in post before
I am talking of absolute path for the script that goes to be included

or may be you’re wrong with dirname/FILE
dirname(/whatever/the/hell/file.php) would return /whatever/the/hell
dirname(/whatever/the/file.php) - /whatever/the
dirname(/whatever/file.php) - /whatever

so, it’s different for the each directory. You cannot call a root such a varying thing.

no not parent script, actual file. You can include config.php from /whatever/somewhere/script.php and FILE will report it’s actual path. Hence you have an abso path and can define a $root … only thing is if you need to look for it, or just require …/config.php

First of all, I hugely appreciate the discussion you guys had above because it shows me all the ways that are viable solutions; in addition, it reveals that this is not a simple topic to deal with.

Secondly, I think for my code, I’m going to try hash’s way of having a root.txt in whatever root folder you choose, building a while loop to find that root.txt, then setting that directory as your root, and building all other absolute paths around that root folder. In this method, you choose your own root, rather than dealing with what you are given by the server.

I have not tried this method yet, but I will soon. The only manual work I could see involved in it is that you would have to put a root.txt into your new host folder (if the host is changed), but that’s not much work at all. PHP-includes, I believe (though have not tested yet), will also work because again, you will be using absolute path names from wherever you specify.

Again, thanks a lot for the discussion and if I have any other problems, I’ll shoot them back to the experts.

PS. Has anyone found a solution for the CSS absolute path names, since I can’t use $root in .css files? Perhaps I have to be more manual about this and change each absolute path name depending on the host (root folder)?

I wrote this solution 2 times already in this topic

why don’t you try it first, if you can’t realize that you have the same web root on the every server in the world?

what’s going to be changed?
image/link/etc become fun/pics/free or what?

Basically, if I work on my localhost, the absolute path name is something like /wamp/www/~sitenameroot

If I upload this website to a virtual host, the absolute path name becomes:
/home1/~something/~username/public_html

This means that everytime I do local testing on localhost and see the changes, I must change all of the absolute paths to visibly see the changes on the virtual host.

Hopefully I’m being clear enough in my explanation.

The easiest solution, of course, would be to use relative path names so that all my files are interlinked despite the changing absolute path. However, I soon as I dabble in PHP-includes, this system falls apart.

The while-loop to find the $root seemed good, but under basic testing (certainly not extensive, so feel free to prove me wrong), it goes too deep into my harddrive C:/

$root = dirname(__FILE__);
while(!file_exists($root.'/root.txt')) {
    $root = realpath($root.'/..')
} 

Upon viewing the source of my file:

<link rel="stylesheet" type="text/css" href="C:/wamp/www/website_name/stylesheets/home.css" /> 

Therefore, home.css is not loading (even though the path name is absolute) because localhost does not recognize its depth (C:/). Perhaps it is because I used the function realpath()?

My main problem is that: if I’m building a website for myself, I can map my own localhost directories to be exactly the same as the one my virtual host uses (eg. create the folders (username)/public_html into my wamp/www folder; it is inefficient, but works, I assume). However, as soon as I give this to a client who throws this up on his own virtual host, the absolute path names are immediately invalid. What I’m looking for is more code flexibility while still being able to use PHP-includes at different sublevels of my directory.

/stylesheets/home.css

You must distinguish your filesystem path from webserver path
/home1/username/public_html/template/ is the filesystem path
but this path doesn’t exist in the virtual web-server.
your browser don’t ever know of it’s existence!
the web-server path begins after public_html.
so, for the browser it will be /template/
so, you don’t need any single variable to make your virtual path absolute. it is always the same
Look at the URL of this very page:
http://www.sitepoint.com/forums/show...76#post4530676
It has protocol part, hostname part and request part, which consists of path, filename and query string.
notice the path?
/forums/showthread.php - <–absolute path. would remain the same if you call it from anywhere. that’s the meaning of the word “absolute”

how on the earth we have PHP developers even with customers, who can’t write even basic HTML?

Considering this is in “header.php”, this will not work because “header.php” is included in different files of different subdirectory levels.

Therefore, for sublevel 1, it may be able to find the stylesheets folder, but as soon as “header.php” is PHP-included into sublevel 2, it will not be able to find the directory.

If I’m not understanding something or being unclear with the exact problem I’m having, please say so.

Links that start with / always go to the document root, and it ignores which directory the user is in.

Alright, here’s an update:

The following does not work:

       			<li><a href="screenprinting.php"><img src="/pages/images/product-screenprinting.png" /></a></li>

The following does work:

       			<li><a href="screenprinting.php"><img src="/tenners/pages/images/product-screenprinting.png" /></a></li>

Ok, so I can change all my absolute paths to have “tenners” in front of them. And now everything works in my local machine.

Now when I place all these files into my virtual host, none of them work because it does not know what “tenners” is. The localhost root for “tenners” is “www” (because it is in a WAMP server), whereas in the virtual host, the root is “public_html”.

This is my dilemma, and I’m looking for a more efficient method as opposed to manually changing the absolute paths every time I upload my site to the Internet.

So far, if hash comes back, I think his $root is the best solution (but I’m not seeming to get it right), and I haven’t tried $_SERVER[‘DOCUMENT_ROOT’] yet, and I think I just proved that / doesn’t work.

The / DOES work, you are just using it on a wrong page.

Are you trying to view the page in your browser using [I]http://localhost/[/I] or C:/…/…/ or /home/…/…/??! That is what it sounds like. You should only ever access the pages through a virtual host (like example.com, dev.example.com). Then using / in ever path will work perfectly, like it does for everybody else.

both $root and $_SERVER[‘DOCUMENT_ROOT’] won’t help you with HTML. Better you understand it someday.

you must have separate virtual servers for the each site on your localhost - it is the only solution

if you don’t have “tenners” on the virtual host - it shouldn’t be on the localhost too

Yes, I am accessing the pages through http://localhost/tenners/pages/index.php.

Should I not be?

And @Shrapnel_N5, could you stop criticizing me about HTML? If you think me not knowing about / (site root, which I know anyway) represents my whole knowledge of XHTML, you are very ignorant. Could you discontinue posting?

No. You should use a virtual host for local development.

Edit: localhost is a virtual host, but not the kind I mean. I mean like dev.tenners.com (if your actual public site is tenners.com)