/    Sign up×
Community /Pin to ProfileBookmark

Access to include($file); in another dir.

Is it possible to have one include directory and access it from sub directories?

I’ll try to explain a little better. If I have the following code:

[code=php]

$file_name = “file_to_access”;
include ($file_name);

[/code]

OK! That’s simple. I know! If I want to access this file but it is in a directory outside of the current one (I need to back out of current, not inside current), what do I write???

I know I will have to change the file name to include said directory but I can’t figure out how to back out of a directory.

In HTML, if you want to back out, you do something like:

[code=html]
“../previous_directory/file_inside.html”
[/code]

By using “../” this backs you out – Can you do this with the PHP function include();?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@CompGeek01Jul 24.2005 — Yup, you sure can.

[code=php]
include("../example/path/tofile.php")

// and

include("../../../example/path/tofile.php");
[/code]


...are all perfectly fine examples of includes. Another option is to use a PHP ini directive setting.

[code=php]
ini_set("include_path","./:../:../../");
include("test.php");
[/code]


...would search the current directory, and the next two up for "test.php" in case you needed to allow flexibility in your file structure.
Copy linkTweet thisAlerts:
@bokehJul 25.2005 — if you want to be able to put the script anywhere but not change the path to the include use 'document root'

For example if you templates were in a directory named templates and that directory was on below root (ie non-public) you could access it like this: [code=php]include($_SERVER['DOCUMENT_ROOT'].'/../templates/include.ext');[/code]
If you call your includes like this it doesn't matter what directory or sub directory your script is in.
Copy linkTweet thisAlerts:
@rch10007authorJul 25.2005 — Thank you both...

I got both ways you've shown to work.

If I use the $_SERVER option I think I can use it no matter which server I am operating on as long as the directory stays the same...HMMM...I guess that holds true for the other way as well.

My problem was that I didn't include enough "../../" to back out of the directory. I wasn't paying attention to my directory structure. Kick me!

OH!!!!

Is there any advantage to using $_SERVER over "../../"????
Copy linkTweet thisAlerts:
@rch10007authorJul 25.2005 — I think I just read the answer to my own question - I get in too big a hurry sometimes, LOL!

(Q) Is there any advantage to using $_SERVER over "../../"????

(A) If you call your includes like this it doesn't matter what directory or sub directory your script is in.

That's a big advantage if your structure gets deep!
Copy linkTweet thisAlerts:
@bokehJul 25.2005 — The big advantage is if the script is moved to a different directory it still works.
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...