Jump to content

Recommended Posts

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..!!

 

 

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.

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.

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 ??

 

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 ??

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.

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 ...!!

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.