views:

95

answers:

2

I have been working on a content management system (nakid) and one of my toughest challenges is the file navigation. I want to make sure the file paths and settings work on local and remote servers. Right now my setup is pretty much something like this:

first.php (used by all pages):

//Set paths to nakid root
$core['dir_cur'] = dirname(__FILE__);
$core['dir_root'] = $_SERVER['DOCUMENT_ROOT'];
//Detect current nakid directory
$get_dirnakid_1 = str_replace("\\","/",dirname(__FILE__));//If on local
$get_dirnakid_2 = str_replace("/includes/php","",$get_dirnakid_1);
$get_dirnakid_3 = str_replace($_SERVER['DOCUMENT_ROOT'],"",$get_dirnakid_2);
//remove first "/"
if(substr($get_dirnakid_3, 0,1) == "/"){
 $get_dirnakid_3 = substr($get_dirnakid_3, 1);
}
//Set some default vars
$core['dir_nakid_path'] = $get_dirnakid_3;
$core['dir_nakid'] = $core['dir_root']."/".$core['dir_nakid_path'];//We need to get system() for this real value - below

The reason I also did it this way is because I want the directory that this program is sitting in to be anywhere on the server ie(/nakid)(/cms)(/admin/cms)

I'm positive I am doing something the wrong way or that there is a simpler way to take care of all this.

If it helps to get a closer look at the code and how everything is being used I have it all up at nakid.org

EDIT: Just realized what I have at nakid.org is a little different than my newly posted code, but the same idea still applies to what I am attempting to do.

+1  A: 

I don't fully understand what you try to get, but maybe getcwd() is what you look for:
http://www.php.net/manual/en/function.getcwd.php

dbemerlin
+1  A: 

By and large, it looks okay to me.

You might want to give the variables more speaking names (e.g. nakid_root_dir, nakid_relative_webroot and so on.)

Remember when converting \ to / in path names: Whenever you match another directory name to one of those settings, you need to str_replace("\\","/"...) in those too.

I don't understand what you aim at with $get_dirnakid_2, though. Why will you screw up my path if I install your application in a directory that happens to be named /etc/includes/php/nakid?

Anyway, you should make those settings user overwritable as well. Sometimes, the user may want to set different settings from what you get from DOCUMENT_ROOT and consorts.

Pekka
I use /includes/php as a way to find out what the install path is (first.php is located in /includes/php). Should I do this a different way?
kilrizzy
I do have a way for the user to change the path in the system, but this initial setup is before any of that takes place
kilrizzy
An afterthought, if this is software you want to publish you may want to get rid of the words "local" and "remote", as they don't generally reflect what you're doing (backslashes are the directory separator on Windows machines, and slashes on Unix/Linux and others, but it could just as well be the other way around, a Windows machine being the "remote" server).
Pekka
Re your first comment: The way it is at the moment, you are replacing *any* occurrence of /includes/php from the path, which is wrong. Re the second comment: It should be fully configurable by the user. What if DOCUMENT_ROOT is not set to anything for some reason? It could happen in some virtual server setups. It must be custom overwritable then.
Pekka
Thank you. I will put some more thought into this
kilrizzy