1

I have a function(orders) with many php includes in a file called extract.php

http://example.com/application/dir1/dir2/dir3/extract.php

function orders ($username)
{
 require ("functions.inc.php");
 define("DATAGRID_DIR", "./../datagrid/");
 define("PEAR_DIR", "./../datagrid/pear/");
 require_once (DATAGRID_DIR . 'datagrid.class.php');
 require_once (PEAR_DIR . 'PEAR.php');
 require_once (PEAR_DIR . 'DB.php');
 require_once ('./../datagrid/classes/database.class.php');

//function code here
}

I have another file(install.php) that calls this function inside the same directory and it works very well.

<?php
include ("extract.php");
orders ("643443876996");
?>

However, when i include it in another function in this path http://example.com/orders/getorder.php, it gives me error that it cannot find my files 'datagrid.class.php';

    <?php
    include ("include('../application/dir1/dir2/dir3/extract.php');");
    orders ("643443876996");
    ?>

How can I get the path referencing correct?

4

2 回答 2

1

来自服务器根目录的引用,使用 $_SERVER['DOCUMENT_ROOT'] 或

需要一次(DATAGRID_DIR。'/application/dir1/dir2/dir3/datagrid.class.php');

ETC...

于 2012-09-10T16:04:21.200 回答
1

相对文件总是从调用 PHP 二进制文件的目录中计算出来的。

为避免这种情况,您可以使用__FILE__常量来引用实际文件。

使用它你可以这样写:

$path = realpath(dirname(__FILE__)) . '/my/relative/path';

include_once $path;

无论在哪里调用脚本,这总是会产生相同的路径

于 2012-09-10T16:16:55.847 回答