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 because the paths are relative to the script that calls the include.
Do I need to create a paths file in every directory??
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.
(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’ve been trying the paths with DIR and it is very convenient. Thanks a lot!
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:
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?
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.