使用绝对路径从任何目录包含PHP脚本中的文件

时间:2012-11-09 23:48:31

标签: php include set-include-path

  

可能重复:
  PHP require file from top directory

我已经使用PHP很长一段时间了,但我总是写它来完成工作,现在我正在尝试学习最佳实践并提高我的技能。现在我想知道从任何目录中将文件包含在PHP脚本中的最佳方法是什么。

我一直做以下事情:

<?php
    // From settings.php
    define('ABSPATH', '/home/dalyn/public_html/');

    // Then I put this at he top of all of my other files

    include 'includes/settings.php';
    include ABSPATH . 'includes/db.php';
    include ABSPATH . 'includes/functions.php';
?>

但如果我的文件在子目录中,我需要做类似的事情:

<?php
    include '../includes/settings.php';
    include ABSPATH . 'includes/db.php';
    include ABSPATH . 'includes/functions.php';
?>

有更好的方法吗?我开始学习OOP,所以如果有一个OOP解决方案适用于所有耳朵。

谢谢。

4 个答案:

答案 0 :(得分:2)

我的应用程序/ Bootstrap.php(对于zend框架应用程序)通常有类似的东西。

define( 'BASE_PATH'          , dirname( __DIR__ ) . '/'                          ); //!< application root directory: note this file is not in the root directory  
define( 'VAR_PATH'           , BASE_PATH        . 'var/'                         ); //!< optional system generated config files
define( 'CACHE_PATH'         , BASE_PATH        . 'cache/'                       ); //!< caches in the filesystem. See ISV_CacheManager
define( 'APPLICATION_PATH'   , BASE_PATH        . 'application/'                 ); //!< where the modules live
define( 'MODEL_PATH'         , APPLICATION_PATH . 'models/'                      ); //!< path to models
define( 'FORM_PATH'          , APPLICATION_PATH . 'forms/'                       ); //!< path to forms
define( 'CONTROLLER_PATH'    , APPLICATION_PATH . 'modules/default/controllers/' ); //!< path to default module controllers
define( 'HELPER_PATH'        , APPLICATION_PATH . 'helpers/'                     ); //!< global zend view helpers
define( 'INCLUDE_PATH'       , APPLICATION_PATH . 'include/'                     ); //!< useful support classes
define( 'DOCUMENT_ROOT'      , $_SERVER['DOCUMENT_ROOT']                         ); //!< http root directory. note _no_ trailing slash.  
define( 'IMAGE_PATH'         , '/image/'                                         ); //!< from the http document root 
define( 'SCRIPT_PATH'        , '/js/'                                            ); //!< from the http DOCUMENT_ROOT

其中一些被送到自动加载器,其他我用于直接包含路径 require_once MODEL_PATH。 '目录/目录/ IncludeFile.php';

答案 1 :(得分:1)

你尝试过自动加载吗? http://php.net/manual/en/language.oop5.autoload.php ..抱歉,我还没有权限发表评论..

更新.. Wesley是对的,您需要将类名与autoload的文件名相同才能工作。我很抱歉你在做OOP-PHP。如果不是自动加载将不起作用,你将不得不以传统的方式做到

function __autoload($class) {
require ABSPATH. $class .".php";
}

ABSPATH中带有classname = filename的任何文件都将被自动加载。

如果您有各种不同文件的路径,则必须使用路径名创建多个常量变量。然后用特定的路径在autoload中调用它们。

例如。

class Myprogram{

public function __construct(){

define('ABSPATH', '/home/dalyn/public_html/');
define('ABSPATH_1', '/home/dalyn/public_html/includes');
define('ABSPATH_2', '/home/dalyn/public_html/some_other_folder');

}

function __autoload($class) {
require ABSPATH. $class .".php";
    require ABSPATH_1. $class .".php";
    require ABSPATH_2. $class .".php";
 }   

}

//Some other file
$myProg = new Myprogram(); // this will define your constants as well as autoload all the required files

如果你没有使用OOP。您必须将不同的路径定义为常量,并按照现在的方式包含文件。 如果你想自动化这个过程

这段代码会有所帮助。

if ($handle = opendir(ABSPATH)) {
  while (false !== ($entry = readdir($handle))) {
     if ($entry != "." && $entry != "..") {
        include_once ABSPATH . $entry;
    }
  }
  closedir($handle);
}

这将包括ABSPATH文件夹中的所有文件。您可以创建一个函数并使用您想要放置的任何路径调用它。

希望这有帮助。

DINS

答案 2 :(得分:0)

当我包含某些内容时,我使用dirname(__FILE__)来获取尝试包含的文件的路径。

<?php
include dirname(__FILE__).'/includes/db.php';

但是如果您使用OOP,我建议您查看一些类加载器,它将在您使用类时自动包含该文件。有一些已经编码的存在,如使用Doctrine和使用Composer的那个。

http://www.doctrine-project.org/

http://getcomposer.org/

答案 3 :(得分:0)

 set_include_path('.'.PATH_SEPARATOR.'your path to includes folder'
            . PATH_SEPARATOR . './library/'
);