Direct Path Names for Hosting Using PHP

I’m having a fairly simple problem which I think can be solved, but I’d like to know if there is a more efficient solution.

When building a website on my personal computer, I use WAMP and the direct path name involves something with “localhost”.

However, when I upload those same files to my virtual host, the direct path names that I had set for images, links, etc. no longer work because the direct path name is not the same. In short, the direct path name depends on the host, in which they are different between my personal computer and the virtual host.

My solution is to assign the full direct path name to a PHP string, and then set that PHP string to everything that needs a direct path name. I will also have to change the direct path name every time between testing on my personal computer and uploading the files to the Internet.

While it’s certainly doable, is there a more efficient solution to this problem? I’d be greatly appreciative. Thanks!

Ah my apologies:

  1. absolute addressing from the web root, for the CSS files, no matter from which catalog it being called

Yes, I see your point, and I did that with my CSS files. However, again we run into the same problem.

If your web root changes (you switch from localhost to virtual host), must you change ALL the absolute image/link/etc. paths?

Right. I asked him how to solve the CSS problem. He said and you agree with using the / for document root, which I get.

But the document root changes, meaning the / is not the same for both hosts, meaning that I must change the CSS paths (eg. absolute paths image files in home.css) each time I upload my files to the virtual host.

Or so I think.

The document root is always just simply /. There is no such thing as “/home/apache/…” or “C:/apache/htdocs” to the browser. Have you actually tried the code? Because I don’t see why it wouldn’t work if you did try it.

You know, I did a while back when I ran into this problem, but a lot of things have changed since then, so I think it would be wise to do that.

If I have more problems, I’ll surely be back.

I spent whole page trying to explain it to hash :slight_smile:

Then you came in, and said:

Using dirname(FILE) is much nicer in my opinion.

But you failed to provide a code that utilizes this language construct in this topic’s context.

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?

Sure, use relative paths instead of absolute. :wink:

I’d say absolute path is obligatory.
But it is very important not to mess URL and path.

So, the best way to handle site paths is to use not URL but path only, absolute one.
“/images/pic.jpg” would be all right on any server.

On the other hand, there is a problem with filesystem paths which, in it’s turn, shouldn’t be confused with site paths.

Surely this exactly what the <base href=“”> element is for?

Another solution is to run a DNS server on your own PC, like Posadis so you can have top level domains on your own pc.

For example you can create a dev.mywebsite.com on your own PC and www.mywebsite.com on the hosted domain. The path’s don’t change then.

Of course you also need apache virtual hosting on your own PC when you this.

I’ve got this set up on my own PC, and it works like a charm :slight_smile:

…or you can just edit the hosts file and achieve exactly the same thing :smiley:

c:\Windows\System32\drivers\etc\hosts - edit with notepad (you need to run notepad as Administrator if on W7 or Vista) and add a line for sub.domain.com or whatever, and the ip address it points to.

All my dev projects happen in subfolders anyway, so my projects are coded to account for this, and allow it to be placed in a subfolder, rather than assuming it will always be run from a top level domain.

Another solution is to run a DNS server

is not a solution but nonsense

Perhaps there is no simple way to do this?

Relative path names would not work because if I’m using PHP-includes, the same header.php would be included in different pages (of different subdirectories).

I’m hoping for a solution regarding an absolute file path.

For example, my host’s directory is similar to:
/home1/username/public_html/website/(then my folder)

My local directory is simply:
/wamp/www/(my website folder)

My solution as of now is to simply assign “home1/username/public_html/website/” to a PHP string like $direct_path_name, and then change it to $direct_path_name = “/wamp/www/” when working on the localhost.

I believe this way will work, it just seems repetitive and inefficient.

For html /images/whatever/stuff.jpg is always going to work.
For php you need either a root to start paths with, or relative paths from the current working directory to where you want to go. If it’s only for includes then you can just set the include path and be done.

To set the path you can either know where you are (eg front controller is always in web root) and build from there, or you can put a marker file in the root and look for it to determine where you are.


$root = dirname(__FILE__);
// /home/~user~/www on one server
// /var/public_html on another
include $root.'/includes/file.php'; //always works

hash, that was precisely what I was looking for. Thank you very much!

Care to elaborate why you think this is nonsense?
I’ve got it set up here with a wildcard TLD and mass virtual hosting. I can just create a folder “myproject” en my DNS server and Apache’s mass virtual hosting automatically allow me to acess that project through http://myproject/
So I have a top level domain and I can use absolute paths for images etc, as I would on http://www.myproject.com/

Why not just code it so it can work anywhere, rather than making the assumption it will always be on a top level domain?

Going to have to agree with Stormrider on this, if the customer can change their mind, they will.

You’ll deploy their application to i-love-bacon.com and before you know it, they’ve changed their domain name to i-love-ham.com and your app blows up.

Code for change, it’s the only thing you can depend on.

I make the assumption the websites I make will run on a TLD bacause out of the 50 or so projects I did so far only 2 of them didn’t (so about 4%).
It’s a good thing to take into account that things change, but if you want to take into account every “what if” you’re making things more complicated than they could be.
In the cases the website is not run on a TLD I know this beforehand and then of course I take this into account.
Anthony, which domain the website runs on is no issue, I could put it on any TLD I like without any changes to code.

I have a similar problem now:

All of my CSS files (because they are included in different sublevel pages) use absolute path names. Since I can’t use $root and PHP in these CSS files, and using relative path names is impossible, is there any solution for this?


Do all websites that have 2 or 3 sublevel directories have this problem (when testing between the local machine and host)? I feel like I am doing something very wrong.

Edit::
This is a small portion of header.php, which is included into many different files on different sublevels of directories.


<?php $root = dirname(__FILE__); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--Meta Tags Go Here -->
<!--Meta Tags Go Here -->
<title>Title</title>

<link rel="stylesheet" type="text/css" href="<?php echo $root ?>/stylesheets/home.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo $root ?>/javascripts/animation.js"></script>
<script type="text/javascript" src="<?php echo $root ?>/javascripts/jquery.ie6.js"></script>


<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="<?php echo $root ?>/styles/ie7.css" />
<![endif]-->

</head>

My problem here lies in that if this page is included on every page of different subdirectory levels, the $root will change depending on where that FILE is located. Therefore, the code will break on certain subdirectories.

At least, I think so. I’d really appreciate any strategies to overcome the CSS difficulty and the PHP absolute path including. Thank you!