juanpablo Posted February 16, 2010 Share Posted February 16, 2010 Hey everyone. I'm completely stuck with this thing. I've tried several ways to make things work, but they all look really ugly to me. The problem is basically the following. Let's say I have the following directory structure: / --- index.php --- include ------- connection.php ------- user.php --- administration ------- index.php So I want to create the following includes: /index.php --> connection.php, user.php /include/user.php --> connection.php /administration/index.php --> connection.php, user.php I've tried the following methods: 1. if(file_exists("include/connection.php"))include("include/connection.php"); else include("../include/connection.php"); 2. while(!file_exists("include/connection.php"))changedir(".."); 3. while(!file_exists("root.path"))changedir(".."); 4. // create a file .relative with the number of folders to go up to get to the root $number = file_get_contents(".relative"); for($i = 0; $i<$number;$i++)changedir(".."); And even 5. include($_SERVER['DOCUMENT_ROOT'])."/include/connection.php"); The problem is, 1-4 look very untidy to me and code may get too complicated to edit in the future. The problem with 5, is that the application will not necessarily be hosted on the document root. What i want, however, is to make sure that the current directory is always set to a given directory, let's say /var/www/mysite.com/. This should work even if i lack administrative privileges on the server. I've searched the web but I couldn't really find a solution to this problem. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/ Share on other sites More sharing options...
idontkno Posted February 16, 2010 Share Posted February 16, 2010 What's wrong with this? include './include/connection.php'; include './include/user.php'; Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1012903 Share on other sites More sharing options...
juanpablo Posted February 16, 2010 Author Share Posted February 16, 2010 What's wrong with this? include './include/connection.php'; include './include/user.php'; The problem is that some files, that I will include will have references to other files. Like this: Running index.php would set the current directory to the application root, so if index.php includes "include/user.php", and user.php includes "./include/connection.php" it will work, why? because the current directory is / However, if I run /administration/index.php and include "../include/user.php", the current directory will be set to /administration and when user.php attempts to include "./include/connection.php" it will fail to find the file, because the current directory has changed. Thanks for such a quick reply! Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1012906 Share on other sites More sharing options...
PFMaBiSmAd Posted February 16, 2010 Share Posted February 16, 2010 the application will not necessarily be hosted on the document root Use #5 but also use an 'application path' variable (or defined constant) in it that you set/define in your application configuration (config.php) file. Something like - include $_SERVER['DOCUMENT_ROOT'] ."/$app_path/include/connection.php"; You could also add entries to the include_path using set_include_path() and let php search for the files, but if you have a large number of files and several different include locations, performance will be better by specifying the actual path to the files being included. Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1012907 Share on other sites More sharing options...
juanpablo Posted February 16, 2010 Author Share Posted February 16, 2010 the application will not necessarily be hosted on the document root Use #5 but also use an 'application path' variable (or defined constant) in it that you set/define in your application configuration (config.php) file. Something like - include $_SERVER['DOCUMENT_ROOT'] ."/$app_path/include/connection.php"; You could also add entries to the include_path using set_include_path() and let php search for the files, but if you have a large number of files and several different include locations, performance will be better by specifying the actual path to the files being included. That's a good idea. However, how can I include my config.php? How to find it? Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1012909 Share on other sites More sharing options...
juanpablo Posted February 16, 2010 Author Share Posted February 16, 2010 So, how can I get to include a certain file, without having to worry about the current directory? Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013339 Share on other sites More sharing options...
taquitosensei Posted February 16, 2010 Share Posted February 16, 2010 set the include path ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); then it doesn't matter what the current folder is it will look in the include folder. you can add more folders to this also. Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013342 Share on other sites More sharing options...
juanpablo Posted February 16, 2010 Author Share Posted February 16, 2010 THANKS! Does this require administrative privileges? Also, does this need to be run on every PHP, or will it be a permanent configuration? Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013345 Share on other sites More sharing options...
taquitosensei Posted February 16, 2010 Share Posted February 16, 2010 no it's a php function so it doesn't require special permissions. This is how I do it. at the top of each page. require_once 'includes/setpath.php'; // '../includes/setpath.php'; depending on what folder I'm in the contents of setpath.php would be something along these lines. ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013347 Share on other sites More sharing options...
juanpablo Posted February 16, 2010 Author Share Posted February 16, 2010 no it's a php function so it doesn't require special permissions. This is how I do it. at the top of each page. require_once 'includes/setpath.php'; // '../includes/setpath.php'; depending on what folder I'm in the contents of setpath.php would be something along these lines. ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); Excellent. Gotta see if that works! Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013349 Share on other sites More sharing options...
Axeia Posted February 16, 2010 Share Posted February 16, 2010 I've found the following quite useful as well. dirname( __FILE__ ) to get the "current" directory of the file you're executing this in, add more dirname functions around it if you need to move up a folder. Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1013377 Share on other sites More sharing options...
juanpablo Posted February 18, 2010 Author Share Posted February 18, 2010 I've found the following quite useful as well. dirname( __FILE__ ) to get the "current" directory of the file you're executing this in, add more dirname functions around it if you need to move up a folder. Cool! This function could come in handy! Quote Link to comment https://forums.phpfreaks.com/topic/192207-best-way-of-handling-include-and-require-paths/#findComment-1014073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.