开发者

How do I refer a file in php which is ahead of two folders

I have the following structure

  folder1
    file1.php
    file2.php
 folder2
    folder2.1(folder)
       file2.1.1(file)

In the above structure how do 开发者_运维问答I refer to file (file1.php) which in folder1 from file2.1.1.php which is in folder2.1


.. takes you up one folder, so (assuming folder2.1 is a child of folder2) you want:

../../folder1/file1.php

This takes you up two folders (to the parent of folder1 and folder2) and then back down into folder1 to find file1.php

(Not sure the slashes are the right ones, but you get the picture)


well, speaking in terms of files

$file = "../../folder1/file1.php"; // worst
$file = $_SERVER['DOCUMENT_ROOT']."/folder1/file1.php"; // better


I suppose you could just use "include" and give it the relative path

 include("../../folder1/file1.php");

or if you know the absolute path and to give it more versatility:

 include("/pathToFolder1/file1.php");


If folder 2.1 is within folder 2: ../../folder1/file1.php

If folder 2.1 is on the same level as folder 1 and folder 2: ../folder1/file1.php


You might want to prepend

dirname(__FILE__)

to the relative pathname, in order to be sure that the relative path will be expanded correctly in every case.

For example:

require_once dirname(__FILE__).'/../folder1/file1.php';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜