Should I use root-relative or document-relative paths when

I plan to design my website on my machine (which has mysql, php, and other things installed) and then plan to move it to various web servers later on?

What do you mean by root relative and document relative? Also where exactly will you be using these paths. Within PHP files to include other PHP files or in client side HTML code.

I will be using them in PHP, when I point to other files not in the document, primarily through includes.
But the question is also for html, or rather pointing to external stylesheets and javascript code.
Root relative = /images/image.png
Document relative (say the current document is in the /text path) = …/images/image.png

Or at least that’s what I think. I am a bit confused myself on this. I read that both have advantages, but I couldn’t tell which was best for portability mentioned above. All I know is that absolute paths are out. I do my development on my own pc, then ftp it to various places.

In my view, by far the best way to to use root-relative links. (Then you don’t have to mess around with the paths on each page.) However, this does pose a problem if you are first developing the site locally. The way around it is to set up a local server environment. I’m not sure if that’s what you have, but the easy way to do this is to use an all-in-one setup that you can download for free. Two for Windows are WAMP and XAMPP. For Mac, there is MAMP.

There are two separate cases here.

  • PHP includes use the file system so it’s best to use the absolute path according to the file system

  • Client side HTML uses the path relative to document root so best to make it root relative

For example say that the document root is:

/home/admin/public_html/

and the website URL is www.example.com

In the case of the PHP scripts what you would first do is define a global constant, usually in the init.php file or some such, containing the document root:


<?php

//    __FILE__ is the full path of the currently executing php file. This assumes that file is in the document root. 
define ('DOCUMENT_ROOT', dirname(__FILE__));


echo DOCUMENT_ROOT ; //displays /home/admin/public_html

echo DOCUMENT_ROOT.'/includes/functions.inc.php'; //displays /home/admin/public_html/includes/functions.inc.php


So you can use the DOCUMENT_ROOT constant + root relative wherever you want the absolute file path such as in the case of php include statements.

I do develop locally on a server environment (like WAMP). I should have been clearer.

Thanks though - I think this solves my confusion. In php I do make use of $_SERVER[‘DOCUMENT_ROOT’] and usually point to files from there. I was a bit afraid that this wouldn’t work if I somehow broke it. I was also a little worried about there being potentially more overhead using this method. I am just trying to aim for maximum portability of files from one server to another (or moving from one folder to another), as the programs and html I develop will be deployed in multiple places.

It should work fine. I’ve never had a problem with it, except that in WAMP etc your document root might be different. You can make the folder your files are in seen as the root folder by tweaking your hosts file.