php relative path in require or include functions

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
domnulnopcea
Forum Newbie
Posts: 5
Joined: Mon Jun 18, 2007 5:09 am

php relative path in require or include functions

Post by domnulnopcea »

can anyone help me???

I have a file named index.php and in this file I include a file from /include folder named a.php! in the /include directory i have a file named b.php! and when i try to include this file from a.php like require ('./include/b.php') it will not include it. The problem is that i have many subdomains!

and from subdomain cantece.resursecrestine.ro i try to include a.php witch is in the /include folder from the root of the site....

when i include ('http://www.resursecrestine.ro/include/a.php) it will include a.php but not b.php which is in the same /include folder from the root. at the same level with a.php

can anyone help me here?

i will really appreciate!

thank you!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

domnulnopcea wrote:when i include ('http://www.resursecrestine.ro/include/a.php)
you send a new http reuest to the webserver just like a browser does thus starting a new php instance that is not connected to your including script.
There is somethin called current working directory. A relative path is relative to this cwd. php set's the cwd to the directory the "primary" script resides in. Therefore the cwd will be /include/ for the php instance executing a.php. And therefore include('include/b.php') tries to include /include/include/b.php
Why do you use include('http://..../include/a.php') ?
domnulnopcea
Forum Newbie
Posts: 5
Joined: Mon Jun 18, 2007 5:09 am

Post by domnulnopcea »

this is the structure of my website

/
include
--------- a.php
---------- b.php
subdomains
---------- subdomain1
---------- subdomain2

when i am executing scripts from subdomain1 let's say index.php i include in index.php something like this include(http://site.ro/include.a.php)..i use the http form because otherwise it will not include it...

i tried to do something like this require(./../include/a.php)...but this will not work...i do not know why..maybe it it smoething with my apache web server that i do not know how to configure....

all i want to do is to make visible the include folder in my subdomains...to share it...so i could acces it...

can you help me?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Use absolute pathes.
$_SERVER['DOCUMENT_ROOT'] should help you with your problem
domnulnopcea
Forum Newbie
Posts: 5
Joined: Mon Jun 18, 2007 5:09 am

it will not work

Post by domnulnopcea »

i have tried ...but DOCUMENT_ROOT will take a path from the subdomain...and i want to acces the root/include
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Hm, now you're confusing me. Exactly what do you mean by subdomain? Because site.ro is no subdomain of resursecrestine.ro
domnulnopcea
Forum Newbie
Posts: 5
Joined: Mon Jun 18, 2007 5:09 am

Post by domnulnopcea »

please forgive me for the confusion

the root is resursecrestine.ro....

i have many subdomains in the subdomain folder of the resursecrestine.ro folder!

the structure is

resursecrestine.ro
----include
-------a.php
-------b.php
----subdomains
---------subdomain1
--------------index.php
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

meaning http://subdomain1.resursecrestine.ro/index.php will request
resursecrestine.ro
----include
-------a.php
-------b.php
----subdomains
---------subdomain1
--------------index.php
?
domnulnopcea
Forum Newbie
Posts: 5
Joined: Mon Jun 18, 2007 5:09 am

Post by domnulnopcea »

in index.php from subdomain1 i require a.php from root...and in a.php i require b.php...

how can i include a.php without the up-level going model (./../../../) and without the http:// form
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please answer my question. Does a request for http://subdomain1.resursecrestine.ro/index.php invoke the script that is marked red in the directory structure?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

include(dirname(__FILE__) . '/../../foo/bar.php');
dirname(__FILE__) starts at the file's current directory.
Post Reply