How to manage paths correctly?

I want to learn how to manage paths correctly.

I have a file ‘paths.php’ inside /home/mysite/public_html with constants containing the relative paths to several files and directories.

The problem is that paths.php is only good if included from a file inside the public_html directory, if paths.php is included from the includes directory all the paths are wrong :confused: because the paths are relative to the script that calls the include.

Do I need to create a paths file in every directory??

Please, help me.

Use absolute paths.

In PHP < 5.3 use:

dirname(__FILE__) . '/some_file.php';

In PHP >= 5.3 use:

__DIR__ . '/some_file.php';

I think I cannot use absolute paths in this case because the includes folder is located outside of the public_html directory

/home/mysite/includes <-includes directory
/home/mysite/public_html

Why do you say it wouldn’t work?

Since you are using relative paths, you can use getcwd() (get current working directory) to determine which working directory the relative paths will be from. If you are in the includes directory, you can prepend a relative path to all your paths in the script to get them to the public directory. If you are in the public html directory, you can prepend a relative path to get you to the includes directory.

The current working directory should be the folder containing the script that first executed; unless you change it. I run everything off index.php (and use .htaccess to create the illusion of SEF urls). That way my current working directory is always the root html folder. But I am using a rather complicated CMS framework too.

Edit: after I said all that, I realized I could have just said Amenthes is right. It would be easier to just prepend Amenthes’s solution to your relative paths. All the relative paths should be based off that path that contains the paths php file.

To include a file relative to the file you’re writing in:


require realpath(dirname(__FILE__) . '/../somefolder/somefile.php');

(You can do without the realpath() if you’re not using ‘…/’ in your path.)

To include a file realtive to the file that’s in your url:

require ‘/somefolder/somefile.php’;

You can also set up include paths, with the set_include_path function. This will make php look for files/folders in your include path as well as relative to the active file.

I got confused thinking I couldn’t use absolute paths to the includes directory because this directory is not inside the public folder. Sorry.

Would this be the correct syntax to define an absolute path?


/*  path.php  */ 
define(PUBLIC_BASE_PATH, '/home/mysite/public_html');

and to include the file:


/*  other files  */
include("/home/mysite/public_html/path.php");
include(PUBLIC_BASE_PATH.'init.php');

Yes, that would work (as long as you make sure you have the correct slashes in the paths).


include(PUBLIC_BASE_PATH.'/init.php'); 
// see the extra slash before init.php


define(PUBLIC_BASE_PATH, '/home/mysite/public_html'); 

As suggested earlier, the above could also be written like so:


define(PUBLIC_BASE_PATH, dirname(__FILE__)); 

This way you don’t have to change the path when uploading changes or moving to a different host.

Use this:

The problem with your define solutions is that you STILL need to hard-code the path to that first file that has the define

I’ve been trying the paths with DIR and it is very convenient. Thanks a lot!:slight_smile:

The problem with your define solutions is that you STILL need to hard-code the path to that first file that has the define

I don’t really understand what you mean. If I am including at the includes directory and want to find the paths.php file in public_html, can I do so with DIR?


/* file in includes directory  */
echo __DIR__; // will return -> /home/mysite/includes  (I'm not 100% sure of this, as I've been testing in localhost

When the file I need to access is at this following path:

/home/mysite/public_html/paths.php

db.inc.php


define(PUBLIC_BASE_PATH, '/home/mysite/public_html'); 

foo.php


include 'db.inc.php';
echo "My path is: " . PUBLIC_BASE_PATH;

That will work, BUT if you move/include foo.php in another script, then you need to change it’s include path.
If you write:


include __DIR__ . '/db.inc.php';
echo "My path is: " . PUBLIC_BASE_PATH;

Then, your PUBLIC_BASE_PATH is useless because you already know the path (DIR + relative path = absolute path.)

So, you can just use DIR

Sorry this is so complicated for me.

In the paths php file I changed all the paths to start with DIR

Edit: after I said all that, I realized I could have just said Amenthes is right. It would be easier to just prepend Amenthes’s solution to your relative paths. All the relative paths should be based off that path that contains the paths php file.

However, then I also used the getcwd method at the “first line” php file (that is included in all files at the beginning) to find the current directory and then used a switch statement to indicate the correct path to ‘paths.php’ from any directory in the site.

I don’t really understand how else to include the paths.php from any directory.

This seems like it may be a bad idea though, what do you think?

try this:


echo __DIR__;

See what that gets you, and you will see that you don’t need getcwd.

I’ve read the thread over a hundred times to see if I’m missing something. Forgive me and thanks for your patience.

I understand why I should use DIR in the paths file, and I changed all the paths to this format.

However, where I get lost is that from any file I still need to include the paths file. If the paths file is one directory above, how do I use DIR to link to the paths file? And why is it better to use DIR as you tell me in this case?

However, where I get lost is that from any file I still need to include the paths file.

Just make sure any script called by the web server (index.php etc) knows where paths.php is:
include ‘…/paths.php’;
Then you’re nested includes can use these paths and be happy.