Page 1 of 1

including files

Posted: Wed May 02, 2007 2:51 pm
by mozartmatt
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Having trouble including files:

i need to include a file at the start of each page.  and then in this included file there are some settings that include other files.  For example:

Code: Select all

<?php

// This is File 1 (Used in different directories for example: content/file1.php, content/my_account/file1.php)

include('file2.php');

// This is File 2 (used in one location: eg. settings/file2.php) 

include('misc_settings.php');

?>
if file 1 is in different directories i can just add ../ to work backwards. The problem is includes in file 2. I can't just add ../ to the includes in file 2 to correspond with File 1, coz file 2 needs can be accessed from different directory levels.

Can anyone understand me, let alone help me? lol

M


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed May 02, 2007 2:56 pm
by arturm
It is a very good practice to use absolute path in your includes

Code: Select all

include ($_SERVER['DOCUMENT_ROOT'].'/content/file1.php');

Posted: Wed May 02, 2007 3:50 pm
by mozartmatt
Hi arturm,

Thanks for the tip! With an hour or so playing about with it, i've managed to fix it!

Appreciate your help!!

M

Posted: Wed May 02, 2007 7:09 pm
by Z3RO21
arturm wrote:It is a very good practice to use absolute path in your includes

Code: Select all

include ($_SERVER['DOCUMENT_ROOT'].'/content/file1.php');
You do not need the () for include. Just a heads up :)

Posted: Wed May 02, 2007 7:20 pm
by arturm
parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.

Posted: Wed May 02, 2007 7:22 pm
by Z3RO21
arturm wrote:parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.
True I try and avoid them to lessen file size (even though it hardly matters) good point though

Posted: Wed May 02, 2007 8:15 pm
by aaronhall
arturm wrote:parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.
include and require are both constructs