Aayush Agrawal
Aayush Agrawal

Reputation: 43

php, requiring files above 1 hierarchy

I am new to php programming, i am very happy that you are taking your time to answer this newbie question. As for my question, is it possible for me to "require" a file thats above a 1 level hierarchy?

What do i mean Lets assume this is a file hierarchy

Folder->(Html, Includes, Templates)->Html->Project1->index.php

Now, how can i access the includes folder from over here? I know that from the HTML folder's index.php I can just do require("../includes/config.php"); but i want to get it from project1's index.php, is it possible? If so, how?

Upvotes: 0

Views: 516

Answers (2)

MrGlass
MrGlass

Reputation: 9262

I'm not sure I understand your question, but if you are looking to reference a folder thats 2 folders above instead of one, you can just use ../../includes/config.php

Whats going on: Essentially, .. is linux shorthand for parent folder. So, by using .. multiple times, you can move higher and higher up in your folder hierarchy, until your in a parent that is shared by the current file and the one you are trying to include.

You can actually use any valid filepath for include, so even a hard reference (eg /var/public/myinclude.php) woudl work. This is very bad practice, however, because it breaks yoru codes portability - if you move it to a different server it will break unless that server is identical.

Upvotes: 1

adamdehaven
adamdehaven

Reputation: 5920

Just add an additional "directory level" ../ for each folder you need to go up. So for project1/index.php, you would do the following:

require("../../includes/config.php");

Upvotes: 1

Related Questions