Problem with includes

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
TwinkiE_HunteR-G
Forum Newbie
Posts: 14
Joined: Tue Sep 17, 2002 8:44 am

Problem with includes

Post by TwinkiE_HunteR-G »

Ok, IM using PWS at home with PHP 4.2.1 on a Windows 98 machine.
My host (Portland Ltd) is running apache on AIX i believe. Now, at home (or work), I am working on a small website. But, whenever I include something in the files at home, it seems that I have to give the relative path to the files that are included in the include file from the file I am including from. Ugh. Let me try to clarify..

All includes are in /Foo/inc

/Foo/Bar/index.php
- includes table.inc
-table.inc includes blah.css

Now, I use include("../inc/table.inc"); and table.inc gives me an error saying it cant find blah.css.
In order for it to see blah.css, I would have to change the include in blah.css to ../inc/blah.css, but I dont want to do that because I want to include tables.inc in other files as well...

On my host, this is no problem. It seems to include from the included files path, not the first documents.

Does anyone understand what Im trying to say? because im pulling my hair out here, I know there has to be an answer, and more than likely the most simple answer.

If you can help, please do! I would greatly appreciate it.

Thanks

-TwinkiE
kng_nothing@yahoo.com
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

the easy solution would be to give the absolute path to the include files.
TwinkiE_HunteR-G
Forum Newbie
Posts: 14
Joined: Tue Sep 17, 2002 8:44 am

Post by TwinkiE_HunteR-G »

The only problem with that is I would have to go through all of my pages and change them every time I wanted to test them on my computer and then upload them to the server. Surley there has to be another way.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I don't think you can use absolute address in include...
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

TwinkiE_HunteR-G,
Link the directory.


Takuma
I don't think you can use absolute address in include...
Why not? Don't think... know or ask.

Direwolf.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I use PWS with Windows 2000 and PHP 4

and here is how I do my includes with no problem

Code: Select all

<?php
require_once('inc/top.php');
?>

how are you including your files? are you saying your trying to include files from included files?
TwinkiE_HunteR-G
Forum Newbie
Posts: 14
Joined: Tue Sep 17, 2002 8:44 am

Post by TwinkiE_HunteR-G »

Ok.
All my includes are in Test/inc
I have Another directory for the site Test/Blah/here
Now, in Test/Blah/here i have a file foo.php
In foo.php i include bar.inc by include("../../inc/bar.inc");
Now, in bar.inc I include another file called woo.inc by include("woo.inc"); since bar.inc and woo.inc are in the same /Test/inc directory.

Now, when I include the file /Test/inc/bar.inc in file /Test/Blah/here/foo.php, I get an error saying it cant find bar.inc

So basically what I need to do is have the include files either
1. include the files they need before including into the first file.
or
2. Have the include files act as if they were in their own directory when being included.
I looked all through the configuration file, and the manual, but couldnt really find anything to address this.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

well hope this helps.. I tested it real quick and heres what I came up with

the subincludes seem to only work going down the directory not back up

so I have a page called MAIN.php

now in MAIN.php I have a include

Code: Select all

<?php
<?php
 include('inc.php');
?>
?>
in INC.php I have

Code: Select all

<?php
<?php
 echo 'Jim1';
 include('test/inc2.php');
?>
?>
in test/inc2.php I have

Code: Select all

<?php
<?php
 echo '<BR><BR>Jim2';
?>

?>
that works fine now if I change that first include to test/inc.php and in test/inc.php I try and do ../inc2.php it fails so it looks like you can't include files within include files if they go back up the directory. You should test it on your system as well, see what you come up with.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

What I meant was that when I tried it before it didn'T work :o
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

You can use "include_path" parameter of php.ini.

PHP looks fo the include()/require()/include_once()/require_once() files in this path.

See PHP man:

:arrow: http://www.php.net/manual/en/printwn/co ... clude-path


For your local php setup, you can set:

Code: Select all

include_path=".:Test/inc"
And from other files (what so ever may be their path) u can simply code:

Code: Select all

<?php
include( 'bar.inc' );
?>

One important thing is, u should always use .php extension for u r include files also. Because if someone types in the complete include file path, he/she will get to see the include file contents.

For u r setup, try this in browser:

http://<yourdomain>/Test/inc/bar.inc

should show up the source code.
Post Reply