include() problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

include() problems

Post by Pyrite »

I can't seem to include php files that are in different directories.

Most of my php files are in:
/cms

And if I include other php files that are in /cms or subdirectories in /cms, no problem.

But I need to be able include files that are in /cms from other directories on the server like:

/language/en/foo/bar/car/

So like file /language/en/foo/bar/car/exam1.php needs to be able to include /cms/includes/config.php (which has the db settings and ADOdb object).

But when I do, I get include() warnings and errors..

I think its my include path in my php.ini, but I can't figure out what to set it to.

I'm using Apache 2.x on Server 2003 with php 4.3.3

Thx.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

so you tried

Code: Select all

include("../../../../../cms/includes/config.php");
?
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Yes, I tried that and include("/cms/includes/config.php"); also. Neither worked.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

When I try including with the absolute path include("/cms/includes/config.php");

I get this errors..

Code: Select all

Warning: main(/cms/includes/config.php): failed to open stream: No such file or directory in E:\www\ibs\language\en\curriculum\courses\romansgalatians\unit02\lesson03\1stQ_Exam.php on line 2

Warning: main(): Failed opening '/cms/includes/config.php' for inclusion (include_path='.;c:\php\includes;\cms\includes;'') in E:\www\ibs\language\en\curriculum\courses\romansgalatians\unit02\lesson03\1stQ_Exam.php on line 2
And when I include with the relative path, I get this:

Code: Select all

Warning: main(../../../../../../cms/includes/config.php): failed to open stream: No such file or directory in E:\www\ibs\language\en\curriculum\courses\romansgalatians\unit02\lesson03\1stQ_Exam.php on line 2

Warning: main(): Failed opening '../../../../../../cms/includes/config.php' for inclusion (include_path='.;c:\php\includes;\cms\includes;'') in E:\www\ibs\language\en\curriculum\courses\romansgalatians\unit02\lesson03\1stQ_Exam.php on line 2
Really, I'd like to be able to use the absolute path include if I can.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

mMm.......get the path of a file with:

Code: Select all

<?php
echo realpath("config.php");
?>
Place, that script where config.php is, then use that path. It should work.

-Nay
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

My config.php looks like:

Code: Select all

<?php
include("extention.inc");

// Include the ADODB DB Class
include('adodb/adodb.inc.'.$phpEx);

// The Database Parameters
$dbhost     	= "localhost";
$dbuser     	= "myusername";
$dbpass     	= "mypassword";
$dbname     	= "ibs";

// Dont edit past here
$db = NewADOConnection('mysql');
$db->debug = false;
$db->Connect($dbhost, $dbuser, $dbpass, $dbname);
?>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Nay wrote:mMm.......get the path of a file with:

Code: Select all

<?php
echo realpath("config.php");
?>
Place, that script where config.php is, then use that path. It should work.

-Nay
Ok, I did that and included the file with:
include("e:\www\ibs\cms\includes\config.php");
And it worked, but the include statements inside of config.php also errored out. Besides, I really don't want to use that kind of include path. If I every move my code, I'll have to change all the scripts. No thanks. Theres gotta be another way.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

First off, I wouldn't know why you would move the config.php. Usually, the config file stays where it always has been.

Second off, here's what you normally do:

Config.php:

Code: Select all

<?php
$path = "e:/www/ibs/cms/includes";
// other stuff
?>
Anotherfile.php

Code: Select all

<?php

include "../../ibs/cms/includes/config.php";

// to include other files

include $path . "otherfile.php";

?>
You'll have to include the config.php file first though, another option is store the path in a MySQL database or so.

-Nay
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

OK. I setup a simular test setup on my other Linux server that uses Apache/php as well.

I created a config.php which defines a variable to "hey man" and put it in /test/cms/includes/config.php

Then created /test/cms/one/two/three/test.php
And it has:
include('../../../cms/includes/config.php');

And it works!
So what is different about my Windows Server 2003 Apache/Php setup? I don't know. Using MySQL for a path is not an option, and neither is using include file paths like e:\path\to\the\web\root\includes\config.php

On my Server 2003 box, I have the include_path in php.ini commented out. For some reason, Php doesn't like me including files that are not under /cms where all my other php scripts are.

May be someone else might have had a simular experience. ?
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Ok, I got it working as I like. I don't really understand it, but it works. And my programming teacher always said, if it works then its right.

Code: Select all

I used this include statement:

include('cms/includes/config.php');

and this include_path in my php.ini

include_path = ".;e:\www\ibs;e:\www\ibs\cms"
Thx for everyone's help.
Post Reply