Forum Moderators: coopster

Message Too Old, No Replies

Include

         

nubbin

3:40 am on May 14, 2004 (gmt 0)

10+ Year Member



I'm giving myself a headache trying to understand why some includes work and some don't. I'm a newbie at php so don't laugh too much if I'm being very dumb:

//1. This include works from a file in my root dir
include ("../hidden_folder/connect_to_a_mysql_db.php");
//indicating I can use relative file names

//2. But this include from a file in my root dir doesn't work
include ("/functions/format_some_text.php");
//indicating I can't use relative file names

//3. Whereas this works
include($_SERVER["DOCUMENT_ROOT"]."/functions/format_some_text.php");

Can anyone explain why 2. does not work?

Thanks

anchordesk

4:52 am on May 14, 2004 (gmt 0)

10+ Year Member



Number 1 is relative so long as the file is correctly placed.

Number 3 is an absolute and would be good in any file anywhere

Number 2 is actually neither. But if number 1 works for you, then changing number 2 to "functions/format_some_text.php" should work if the file with the include statement is located in the "functions" folder.

nubbin

8:32 am on May 14, 2004 (gmt 0)

10+ Year Member



Anchordesk,
Thanks for your explanation.

I want to include a file in a sub folder rahter than the same folder. I want to use a relative reference as it is easier to read.

What should the relative include statement look like if the file containing the include is /root/file_containing_include.php
and I want include file /root/functions/format_text.php

Thanks
Nubbin

anchordesk

2:32 pm on May 14, 2004 (gmt 0)

10+ Year Member



If your server is set to allow relative paths ...

include './functions/format_some_text.php';
include 'functions/format_some_text.php';
require './functions/format_some_text.php';
require 'functions/format_some_text.php';

They should all work.

corz

12:05 am on May 17, 2004 (gmt 0)

10+ Year Member



include ('path/some.file'); // and
include ('./path/some.file');

always seemed the most reliable ways to do this on different server configs. document_root is a fairly reliable starting point. I often do..

$path = $_SERVER["DOCUMENT_ROOT"];

early on, which is handy, and easy to remember when you do..

include ($path.'/some/path/some.file');

later on. a cooler way to do it..

$root = $_SERVER['DOCUMENT_ROOT'];
$path = substr($_SERVER['PHP_SELF'],0,(strrpos($_SERVER['PHP_SELF'],'/')+1));

once these veriables are established (maybe in a config.php), it's real easy to remember something like..

include($root.$path.'some.file');

whenever you need a reliable path. this make things totally portable, too.

I'm fairly new to php myself, enjoying it, I've found this stuff useful, maybe some newbie browsing this thread would find useful too.

;o)
(or