View Full Version : The path of my application?

09-12-2009, 01:46 AM
Web application programming still plagues me, basic things such as this... What is the best method to reference the path of where my application is installed?

The path could vary...

Also the name of the "my_app" directory could vary...

My application has a few sub directories with files that need to include a common file, but I'm not sure how to go about writing the include statement so it will work regardless of the directory structure. I have looked at the manual, but if the answer is there it was lost on me. Any help is appreciated.

09-12-2009, 01:59 AM
I'm not really sure what you mean exactly... I guess depending on how complex you want to get there are lots of different ways. The simplest method I use is to create a variable that contains the path back to the base of all your files. From there I have predefined folders to store my programming in.

// How ever many folders you need to go back on a particular script. One folder back.
$base = '../'; // For two folders back '../../' and so on.

// Define my programming folders.
define ('SCRIPTS', 'Scripts/');
define ('PRESENTATION', 'Presentation/');

// Which basically finds the file one folder back in the scripts folder with the name.
include $path . SCRIPTS . 'my_database_connection.php';

// Some Code

// Which basically finds the file one folder back in the presentation folder with the name.
include $path . PRESENTATION . 'template_for_page.php';

So if this page was on your server at yoursite.com/folder_1/this_script.php

You would place the $base variable equal to '../' if your includes where in the root and '../../' if they were behind the root.

09-12-2009, 02:19 AM
Thanks Coyote, I think you got my meaning. Your example is fine when the actual directory structure is known. The issue is that some user might install it on the root and another user might install it 2 or 3 directories deep. If I have a way to find the base directory of my application, it's easy sailing... just not sure how to do that.

I'm thinking this must be a common issue for those making web apps (whatever you want to call it) that they distribute, no? I have never done this before. In the world of VB/VC it's like calling app.path... returns the path of the application regardless of where it's installed.

09-12-2009, 04:32 AM
define('PATH', dirname(dirname(__FILE__)));

should work

09-15-2009, 01:51 AM
Thanks Zangeel, I wrote a lengthy reply, then erased it in lieu of the following simple question.

If I execute a script from my application's "install directory" to get the dirname of that location how do my other scripts get access to that data? Even if I define it as a constant I must then include that script into all other scripts that need access to the constant, correct? The only other option I know of is to put it into a session variable... but this seems like a hack.

09-15-2009, 01:59 AM
Let's say you're three folders up from your public_html i.e. public_html/folder1/folder2/folder3(you're here)

define('PATH', dirname(dirname(__FILE__)));

echo PATH . '/';

Will show

/home/{username}/public_html/{folder1}/{folder2}/<<directory you're in (doesn't show)>>

Put it a folder up again it shows

/home/{username}/public_html/{folder1}/{folder2}/{folder3}/<<you're here so it doesn't show this folder name>>

Straight forward, so you can define this constant in any file, such as install files that you need a to find a path in.

09-15-2009, 02:24 AM
Thanks again sir, but I'm going to have to come back and post the long version... please bare with me :)

Until I can get back and do that, I'm not after the path of the working directory, I'm after the path of the first directory no matter what my working directory is. I will come back to clarify, but I have to run right now...

09-15-2009, 01:39 PM
Haha, OK, well you can always just explode the file like

$e = explode('\\', dirname(dirname(__FILE__)));


(Testing n my localhost, so I'm using \ instead of /) but this gives this output:

0 => string 'C:' (length=2)
1 => string 'Users' (length=5)
2 => string 'ComputerName' (length=6)
3 => string 'AppData' (length=7)
4 => string 'Local' (length=5)

So you can put the differnt parts of the path in the url. So if you wanted to find a file from public_html you'd start with something like

$path = explode('/', dirname(dirname(__FILE__)));

print $path[0]; // home
print $path[1]; // username
print $path[2]; //public html