worth2talk Posted April 8, 2007 Share Posted April 8, 2007 Hi PHP Experts, I have been hanged badly with a problem of including PHP file from upper level directory. My folder structure is as below: My main folder name is "PHP_Main" which contains for files as - menu.php - header.php - body.php and - footer.php Also "PHP_Main" have few subfolders as - Post1 - Post2 Again Post1 contains file called postdata.php. Now, i want to include menu.php, header.php and footer.php inside postdata.php. Can anyone guide me how to include this.... i am left with no clue... Immediate help will be appreciated... Thanks in advance..!! Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/ Share on other sites More sharing options...
MadTechie Posted April 8, 2007 Share Posted April 8, 2007 erm... ../menu.php ../header.php ../footer.php Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224025 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 include('../file.php') will include file.php from one directory level up, basically, the farther you want to go up, the more ../'s you use. I find that using $_SERVER['DOCUMENT_ROOT'] is the best, becuase that is your root directory, and you go down from there. So, you could do this: include('../menu.php'); include('../header.php'); include('../footer.php'); OR include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/menu.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/header.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/footer.php"); This asumes that PHP_Main is in the root directory. Either of these will work, both have advantages and disadvantages, I would recommend the second set, but its up to you. Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224027 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 erm... ../menu.php ../header.php ../footer.php This i already tried, but not working...!! ??? Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224032 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 Post your directory structure starting from PHP_Main, like this: PHP_Main -menu.php -header.php -body.php -footer.php -Post1 --postdata.php -Post2 etc. Please, since ../main.php does go up one level from the directory you are currently running your script from, so if you are running from Post1 which is in PHP_Main and menu.php is in PHP_Main, then doing include('../main.php') will work. Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224034 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 include('../file.php') will include file.php from one directory level up, basically, the farther you want to go up, the more ../'s you use. I find that using $_SERVER['DOCUMENT_ROOT'] is the best, becuase that is your root directory, and you go down from there. So, you could do this: include('../menu.php'); include('../header.php'); include('../footer.php'); OR include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/menu.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/header.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/footer.php"); This asumes that PHP_Main is in the root directory. Either of these will work, both have advantages and disadvantages, I would recommend the second set, but its up to you. Heyyy HaLo2FrEeEk, Thanks for your suggestion.. your 2nd idea works correctly.... One mistake i made is its more than 1 level ... i mean my directory structure is as below: PHP_Main |_menu.php |_header.php |_body.php |_footer.php |_Post2 |_Post1 |_PostInfo |_postdata.php so its 2 level and may be due it this it fails to include('../menu.php'). I m just curious as u mentioned that "include('../file.php') will include file.php from one directory level up, basically, the farther you want to go up, the more ../'s you use." How to achieve this without using _SERVER ?? Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224036 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 If it is two directorys up, use this: include('../../menu.php'); Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224038 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks again, it works.... u did a gr8 help for me...!! Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224046 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 include('../file.php') will include file.php from one directory level up, basically, the farther you want to go up, the more ../'s you use. I find that using $_SERVER['DOCUMENT_ROOT'] is the best, becuase that is your root directory, and you go down from there. So, you could do this: include('../menu.php'); include('../header.php'); include('../footer.php'); OR include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/menu.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/header.php"); include($_SERVER['DOCUMENT_ROOT'] . "/PHP_Main/footer.php"); This asumes that PHP_Main is in the root directory. Either of these will work, both have advantages and disadvantages, I would recommend the second set, but its up to you. Btw, what is the disadvantage of 2nd method that uses _SERVER ?? Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224047 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 Its more typing that way, sometimes ../ is better, but, say you want to call up a page from 3 different locations on your server, a stats page for example. if you use ../stats.php, wherever you call it up from it will think that you mean ../ relative to that location, if you use $_SERVER['DOCUMENT_ROOT'] it will know exactly where the page you are looking for is located. ../ is mostly guesswork, that is why I prefer the $_SERVER method. Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224056 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 heyyy thanks for explanation... As a being human.... need one more help... now the problem is, i have a image file say logo.jpg ... located in PHP_Main as below: PHP_Main |_menu.php |_header.php |_body.php |_footer.php |_routines.php |_ logo.jpg |_Post2 |_Post1 |_PostInfo |_postdata.php Now, i have one file say routines.php that stores all PHP routines i need to display posts and this routines.php is included in postdata.php as well as body.php. One of the function of routines.php is being called from postdata.php as well as body.php which display logo.jpg image. When i called the function from body.php it works fine and display logo.jpg on the page as image resides in the sam directory, but when i call the same function from postdata.jpg, it wont display that image, becuase image is not in the same directory. So i want to know, how to achieve this ? One way is to pass the path of image as an argument to that PHP function, but is it the correct way ? is there any better way to do this ? Thanks in advance ...!! Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224428 Share on other sites More sharing options...
tarun Posted April 8, 2007 Share Posted April 8, 2007 <?PHP $host = $_SERVER['HTTP_HOST']; include("http://$host/PHP_MAIN/menu.php"); ?> I Used To Use That Untill My Webhost Disabled URL Inclusion Edit: I Forgot The [ code ] So Ive Added It Now Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224434 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 This is where $_SERVER comes in. Instead of including the image in the same directory like this: include('./logo.jpg'); Do this: include($_SERVER['DOCUMENT_ROOT']."/PHP_Main/logo.jpg"); That way no matter where you call it from the site, it knows that it is in root/PHP_Main, ./ and ../ are relative, so postdata.php think you are calling logo.jpg fromt he same folder as it is in. Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224441 Share on other sites More sharing options...
worth2talk Posted April 8, 2007 Author Share Posted April 8, 2007 But the way i am displaying the image is as below: $imageDisp = "<img src=\"logo.gif\" height=\"11px\" width=\"11px\" />"; echo "$imageDisp"; So how to achieve this ? Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-224481 Share on other sites More sharing options...
worth2talk Posted April 9, 2007 Author Share Posted April 9, 2007 Can anybody help me ??? Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-225198 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 9, 2007 Share Posted April 9, 2007 I'll help you, but for future reference, this site isn't for people to write other people's code, its for asking for help with problems that the person made an honest effort to do and couldn't. I also asked you to post your directory structure from root so I knew what I was dealing with, for now, I will assume that PHP_Main is in the root, and logo.gif is in PHP_Main $imageDisp = "<img src=\"{$_SERVER['DOCUMENT_ROOT']}/PHP_Main/logo.gif\" height=\"11px\" width=\"11px\" />"; echo "$imageDisp"; That should get the job done. Quote Link to comment https://forums.phpfreaks.com/topic/46105-help-how-to-include-files-from-upper-level-directory/#findComment-225475 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.