Query: Can you 'include' a file that is up a level?

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
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Query: Can you 'include' a file that is up a level?

Post by randomblink »

For isntance...

Here is my directory:
[root folder]
+ data.php
+ user.php
+[FolderX]
++ index.php
++[FolderY]
+++test.php

How would I INCLUDE the data.php which is TWO LEVELS HIGHER?
I tried doing something like:
include("../../data.php");

But it wouldn't reach it...
Can someone help? Thanks...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just set the include_path in php.ini, a vhost section of httpd.conf or using ini_set() to /path/to/rootfolder then you can require/include 'data.php' in any file regardless of which directory it's in.
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Post by randomblink »

Wha?

Forgive the eloquence of my reply... but Wha?!

These are all new to me...
I do know that I can't access alot of php settings cause those are handled by my webhost... Can I set this through straight php in a php file?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well as you're on a webhost, try this. In a file called .htaccess put:

php_value include_path /the/full/path/to/the/root/folder

Put the .htaccess directory in the root folder. This should allow you to require/include data.php in any file, from anywhere. The include_path sets the path to be searched when you use include or require.
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Post by randomblink »

Bear with me...

When you say put the /full/path/to/the/root/folder in this .htaccess file, do you mean like this?

/root folder/

Or do you mean the path to the root folder FROM where the include file is?

/../../root folder/

or do you mean FROM the root folder TO the file that is INCLUDING...

/root folder/FolderX/test.php/

I am sorry to be so basic here, I just am confused...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When you tried to

Code: Select all

include('../../data.php');
did it output an error or anything? If it can't reach it, usually the error that is output is something like it cannot find the file or permission denied. Try echoing something from the data.php file so you can see when it is included.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

When you say put the /full/path/to/the/root/folder in this .htaccess file, do you mean like this?
I mean the full web directory path, eg /home/fred/www/root .. basically whatever echo $_SERVER['DOCUMENT_ROOT'] returns.
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Post by randomblink »

Ok. I entered the:

php_value include_path /the/full/path/to/the/root/folder

I cut and pasted the above line and copied over the:

/the/full/path/to/the/root/folder

with the output from a:
echo $_SERVER['DOCUMENT_ROOT'];

command... The command was run from a php file IN the root folder... (if that matters) But I ran it, cut and pasted it over the:

/the/full/path/to/the/root/folder

line... SO hopefully this works...!
Thanks for all your help...
Post Reply