PHP 之目录包涵

PHP网站中可经常用到一些公共的文件如站点配置文件、数据库连接文件、函数(类)文件等,正常情况下我们一般使用相对目录的方式,但如此行行事目录一旦复杂、深层将会使调用出现意外的麻烦。 故以下方法可以考虑使用: 1.$_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''); ?> 2.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. 如果你每个脚本都需要包含一个通用脚本的话,这几乎是最好的方式,但是,缺点还是,与配置相关,不够独立。 ========== php.ini文件中的include_path设置 ============= 1. 找到php.ini配置文件修改php.ini的include_path选项, 2. 提醒一下,php.ini中一共有两处include_path,一处是Unix下使用的,一处是windows下使用的,要修改windows下使用的: ------------------------------------------------------------- ; Windows: "path1;path2" ;include_path = ".:/php/includes" 修改为: ; Windows: "\path1;\path2" include_path = "D:\Appserv\www\Smarty\libs" 保存即可,这个是PHP的包含文件路径,可以设置多个,用";"格开。 ------------------------------------------------------------- 一:修改php.ini文件中的include_path项。 例:include_path = .:/usr/local/lib/php:./include 二:使用ini_set方法(对于无法修改php.ini的情况)。 例:ini_set("include_path", ".:../:./include:../include include_path补充 include_path用来设置include()或require()函数包含文件的参考路径. 也就是说当使用include()或require()函数包含文件的时候,程序首先以include_path设置的路径作为参考点去找文件,如果找不到,则以程序自身所在的路径为参考点去找所要的文件,如果都找不到,则出错. 当include_path设置了多个参考路径(每个路径用分号隔开)时,排在前面的路径优先找. 例如: ini_set('include_path','d:\www\mysite\test;d:\www\mysite\test2'); include('aaa/test.php'); 程序会先以路径d:\www\mysite\test为参考去找aaa/test.php,若没有,再以d:\www\mysite\test2为参考去找,再没有,则以自身所在路径为参考去找. OTHER: auto_prepend_file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值