tags:

views:

87

answers:

5

in my config.php where i have all constants i set the PATH to a absolute path.

but this means that when i move my application folder i have to change this path.

i wondered if its better to set a relative path, in that way whenever i move my application between production and development folder, i dont have to change it.

how do you guys do when you move between folders?

+3  A: 

__FILE__ is your friend.

Ignacio Vazquez-Abrams
+1  A: 

IMO, absolute paths are bad news. Even if you don't plan to move, your hosting provider could move you, like DreamHost recently did to me. I was fine.... But there are 14 references to "path" on their wiki: http://wiki.dreamhost.com/Server_Moves

Chris Thornton
This is why the constant declaration was relative to __FILE__, which will always be valid, even if the server moves physically.
Christian Mann
+7  A: 

The best way I've found is to do the following:

define("PATH", realpath(dirname(__FILE__)));

That gives you the directory of the current file. If you do this in your settings/bootstrap/init file, you'll have it available to your application, and it will work for any file system.

zombat
My method is similar, except the nesting of the functions. Does it matter, or have I overlooked something?
alex
Ha ha, nice. No, it shouldn't matter.
zombat
what does realpath mean? and what happens without it?
never_had_a_name
`realpath()` calculates the correct path to a file, resolving any path things like `..\ ` http://php.net/manual/en/function.realpath.php
alex
i thought that dirname() also gives you absolut path. but it gives you relative path?
never_had_a_name
`dirname()` only returns the directory portion of a file path string. So if you did `echo dirname("../../x.php");`, you would get `../..` as output. `realpath()` will convert the relative portions of a file path into the actual hard path.
zombat
+3  A: 
define('BASE_PATH', dirname(realpath(__FILE__)));

This will make your scripts more portable.

Include a file like this

include BASE_PATH . 'includes/header.php';
alex
+1  A: 

I do three things to solve this:

  1. The first is to use paths relative to the current file and include things using dirname(__FILE__).

  2. The second is to use a loader include that all the pages load. This file has one responsibility: to find the include directory, usually via a relative call. So long as this relative relationship stays, it doesn't need changing.

  3. I also like to support custom settings that belong to the installation rather than the codebase. This is done by an include mechanism and overrides a few settings that will be specific for the server the code is on.

staticsan