vividona Posted April 27, 2009 Share Posted April 27, 2009 Hi friends, I don't know how to explain this. I am confusing when regarding including files. suppose I have these stuff in my script: folder1/test1.php folder2/test2.php index.php if I need to include test1.php file to be inside test2.php file, I use this function require"../folder1/test1.php"; // (correct right???). if I need to call test2.php to be available in index.php, I use this require("folder2/test2.php"); // (correct right???). but the problem it will refuse the first path which we include first. require"../folder1/test1.php"; bcs "../" means from folder to folder how can I make all paths readable??? Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/ Share on other sites More sharing options...
fantomel Posted April 27, 2009 Share Posted April 27, 2009 let's pretend that you have this folder structure applications/index.php test/config.php to include the config file you would do something like this: include_once('../test/config.php'); where ../ = one folder bellow more informations about include_once require_once include and require you can find on http://www.php.net/manual here it so for the include_once http://www.php.net/include_once if you have two files in folder let's say: applications/test.php applications/index.php you will do like this include_once('index.php'); in test.php file Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820489 Share on other sites More sharing options...
gnawz Posted April 27, 2009 Share Posted April 27, 2009 I'm not sure I understand you. But for include, Just use ../.../yourfile.php use ../ to depict a folder location Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820491 Share on other sites More sharing options...
nankoweap Posted April 27, 2009 Share Posted April 27, 2009 i'm a relative noob to php, but i've had zero issues with includes/requires when the paths are relative from the app's document root. i figure if it's going to search for includes from one or more directories, the doc root will be one of them. so far so good. try making your paths relative to the doc root and see what happens. jason Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820492 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Include locations are relative to the script being executed, not the one being included. Which is why using absolute path's (they can be dynamic using $_SERVER['DOCUMENT_ROOT'] are often the better way to go when including a file. Your issue here is a prime example of why you would want to use absolute paths instead of relative. Meaning that if you are including test2.php inside of index.php any includes in test2.php would have to act like they are coming from the folder that houses index.php if that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820493 Share on other sites More sharing options...
fantomel Posted April 27, 2009 Share Posted April 27, 2009 I'm not sure I understand you. But for include, Just use ../.../yourfile.php use ../ to depict a folder location that means two folders below Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820495 Share on other sites More sharing options...
reQuorter Posted April 27, 2009 Share Posted April 27, 2009 Hi friends, I don't know how to explain this. I am confusing when regarding including files. suppose I have these stuff in my script: folder1/test1.php folder2/test2.php index.php if I need to include test1.php file to be inside test2.php file, I use this function require"../folder1/test1.php"; // (correct right???). if I need to call test2.php to be available in index.php, I use this require("folder2/test2.php"); // (correct right???). but the problem it will refuse the first path which we include first. require"../folder1/test1.php"; bcs "../" means from folder to folder how can I make all paths readable??? First of all, why your require syntax is different in those 2 scripts (you use only "" and then (""))? Just stick to one syntax, it's just better for reading the code. When you are using "../" it goes outside the folder you are right now, so ex. you have "server/public/folder1" and your script is in "server/public/index.php", the command "../" says go to "server/" folder. Just corrent first one without "../" so: require"folder1/test1.php"; // Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820496 Share on other sites More sharing options...
vividona Posted April 27, 2009 Author Share Posted April 27, 2009 yes fantomel you are right applications/index.php test/config.php main.php will be like this include_once('../test/config.php'); but what if I need to call this again to the main.php??? it will fail right??? Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820498 Share on other sites More sharing options...
vividona Posted April 27, 2009 Author Share Posted April 27, 2009 Include locations are relative to the script being executed, not the one being included. Which is why using absolute path's (they can be dynamic using $_SERVER['DOCUMENT_ROOT'] are often the better way to go when including a file. Your issue here is a prime example of why you would want to use absolute paths instead of relative. Meaning that if you are including test2.php inside of index.php any includes in test2.php would have to act like they are coming from the folder that houses index.php if that makes sense. hiiiii at last I found someone understand me plz explain more Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820500 Share on other sites More sharing options...
vividona Posted April 27, 2009 Author Share Posted April 27, 2009 do you mean include $_SERVER['DOCUMENT_ROOT'].'/folder1/test1.php'; Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820505 Share on other sites More sharing options...
fantomel Posted April 27, 2009 Share Posted April 27, 2009 yes fantomel you are right applications/index.php test/config.php main.php will be like this include_once('../test/config.php'); but what if I need to call this again to the main.php??? it will fail right??? i'm not sure where you want to point but we use include_once to include a file only once so yes if you include it again you will get an error :-? Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820507 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 hiiiii at last I found someone understand me please explain more In general I have a config.php file that houses all pertinent path information for my script by defining a constant. (This would be in the config.php) define("DIR_MAIN", $_SERVER['DOCUMENT_ROOT']); So in general I always include this "config.php". example of index.php <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/config.php"); include(DIR_MAIN . "/folder2/test2.php"); ?> Example of folder2/test2.php <?php include(DIR_MAIN . "/folder1/test1.php"); ?> Example of folder1/test1.php <?php echo "You have reached test1 from test2.php inside of an index file!"; ?> Folder structure: index.php includes\config.php folder1\test1.php folder2\test2.php Should be what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820509 Share on other sites More sharing options...
vividona Posted April 27, 2009 Author Share Posted April 27, 2009 Thank you premiso the proble is solved include $_SERVER['DOCUMENT_ROOT'].'/folder1/test1.php'; Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820511 Share on other sites More sharing options...
vividona Posted April 27, 2009 Author Share Posted April 27, 2009 hiiiii at last I found someone understand me please explain more In general I have a config.php file that houses all pertinent path information for my script by defining a constant. (This would be in the config.php) define("DIR_MAIN", $_SERVER['DOCUMENT_ROOT']); So in general I always include this "config.php". example of index.php <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/config.php"); include(DIR_MAIN . "/folder2/test2.php"); ?> Example of folder2/test2.php <?php include(DIR_MAIN . "/folder1/test1.php"); ?> Example of folder1/test1.php <?php echo "You have reached test1 from test2.php inside of an index file!"; ?> Folder structure: index.php includes\config.php folder1\test1.php folder2\test2.php Should be what you are looking for. ya I got the idea now the problem is solved thank you again Quote Link to comment https://forums.phpfreaks.com/topic/155879-solved-about-including-files/#findComment-820512 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.