当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

网友答案:

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.

网友答案:

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");
分享给朋友:
您可能感兴趣的文章:
随机阅读: