登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: php include 路径 dirname(__FILE__)     [回主站]     [分站链接]
标题
php include 路径 dirname(__FILE__)
clq
浏览(0) + 2010-02-09 15:22:24 发表 编辑

关键字:

很麻烦
clq
2010-2-9 15:23:06 发表 编辑

PHP中include路径的解决方法汇总 [zt]

这 几天整理一份很乱的代码,这才意识到php对include处理不是一般的贱:别的编程语言在处理include中的相对目录时,都是以当前处理的文件作 为基准。也就是说,如果A包含B,B包含C时,C再包含一个含相对路径的文件,那么路径是相对于C的。这样的处理很自然,符合人们的直觉,也便于开发出路 径无关的程序包。

可是PHP不这样,它优先相对工作目录来处理,并且如果路径中包含. ..的话,则只相对于工作目录。
也许PHP这样处理有它的理由,有谁知道的不妨告诉我。

下面是解决这一问题的几种方式:

  • __FILE__

__FILE__ always equals to the real path of a php script regardless whether it's included.

 

 

__FILE__ helps you specify the file to include using relative path to the including file.
这种方法首选推荐。虽然你的include语句会因此要写得长一些,但是一个字,值!

<?php 本文来自:http://www.87717.com

include dirname(__FILE__).'/subdir';

 

//dirname return value does not contain the trailing slash

 

?>

 

 

  • $_SERVER['DOCUMENT_ROOT']

This method allows you to specify a path relative to the web server doc_root for file inclusion.
这也是许多项目在采用的一种不错的方式,就我看来,缺点是,整个项目不方便移动。

例如你一开始放置在xxx.com/,后来需要放到xxx.com/abc/下的话,你要改文件(在一个公有文件中计算ROOT的位置,其他文件包含这个共有文件)。
特别是当你同一份代码放多处时(例如一个测试环境和一个正式环境),你改文件也不好改。

 

<?php

if (!defined("WETSITE_BASE_DIR"))

define("WETSITE_BASE_DIR", $_SERVER['DOCUMENT_ROOT'].'/Clare/');

 

require_once(WETSITE_BASE_DIR.'includes/global.inc.php');

?>

  • chdir()

The include looks for file relative to current working directory. We can use this feature. It's really a "fancy" way, but I'm not sure whether it's safe all the time. Who knows?
这种方式感觉稍嫌麻烦了点,随时要记得恢复工作目录也不是容易的事。写完这句话后,我随后写了几个测试文件,发现这种方式的最重要缺点不在麻烦,而在它的副作用:改变了工作目录,这会导致程序逻辑出错。

rainfalling at yahoo dot com (21-Sep-2005 01:06)

 

This is yet another way to include files relative to the current file. I find it easier if you have a lot of includes.

<?php

 

$prewd = getcwd(); // get the current working directory

chdir(realpath(dirname(__FILE__))); // change working directory to the location of this file 

include('includedfile.php'); // include relative to this file

 

chdir($prewd); // change back to previous working dir

?>

  • set_include_path()

This way is the most convenient way but it's not without flaws. First, not in all cases you have permission to change server configuration. Second, if there are many path specified in include_path, the actually included file may not be the one you expected because there may be files of the same name under different directories.
这是最方便的方式,但不是没有缺点。首先,有时候你不见得有权限修改配置。其次,当不同路径下的文件名有重复的时候,你会被搞糊涂的(就算你不会,你的维护者呢)。

  • auto_prepend_file and auto_append_file in php.ini

This almost the best way if your scripts commonly need a startup script. We can do a lot of useful things in the startup script, for examples, define constants, load configurations. But it's not always OK to change the php.ini settings. Remember the most adaptive application should be as independent from configs as possible.  
如果你每个脚本都需要包含一个通用脚本的话,这几乎是最好的方式,但是,缺点还是,与配置相关,不够独立。

发表于 @ 2008年01月08日 13:55:00


clq
2010-2-9 15:25:16 发表 编辑

<?php

include_once(dirname(__FILE__).'/../hpublic/include.php');
include_once(dirname(__FILE__).'/../obj_bbs/include.php');


?>
这个是我用的。

clq
2010-2-9 15:28:13 发表 编辑

__FILE__  原是 c/c++ 中的当前文件名,在这里变成了全路径名,加个 dirname() 调用,相当于 delphi 中的 extractfilepath()  不过没有最后的那个 "\"  。这样说应该很明白了。我看好几个著名的公用类库都是这样用的。


总数:3 页次:1/1 首页 尾页  
总数:3 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1