用 ini_get('include_path');get_cfg_var ( string varname ),可以获取php的include_path 的值, 用 ini_set('include_path',目录);,别名:ini_alter,可以设置php的include_path 的值. 目录可以是相对路径,也可以是绝对磁盘路径, ini_get() return the current value of the configuration option. But
the get_cfg_var() always get the value from config file, php.ini.
例: <?php
ini_set('include_path','./libs'); echo ini_get('include_path'); //./libs
ini_set('include_path','D:/mysite'); echo ini_get('include_path'); //D:/mysite
?>
|
这样就动态的添加了PHP的include_path. 一般情况下,我们用
ini_set('include_path',ini_get('include_path').';D:/mysite');
| 注意情况: 比如有这样的目录结构: D:/my + | +--test-+ | | | +--testdir-+ | |--testdir.php | |--test1.php |--test2.php 我们设 include_path = 'D:/my' 然后,我们可以 require('./test1.php'); require('./testdir/testdir.php'); 但是如下是错的: require('/test1.php'); require('/testdir/testdir.php'); include_path只是把目录设为包含目录,单不是顶级目录.举例: 我们要使用PHPUnit或zend Framework,但是他们所有的require都是 require 'PHPUnit/dir/filename.php'这样,我们站点下如果只有一个地方放者堆东东,但想从任意目录访问,这样有点麻烦了,请君看: <?php
/* 获取系统所在目录 比如 *nix的/usr/www/ 或win的 E:/www/ */
define('CMS_ROOT',str_ireplace('\\','/',dirname(dirname(__FILE__))).'/');// 用两次dirname的原因是,我的文件放在根目录的下一层目录
/**
* 把目录设为php的include_path
* 比如站点在目录E:\web
* 把E:\web\libs添加到include_path,只需 setIncludePath('libs');
*
* @param string $path
*/
function setIncludePath($path) {
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . CMS_ROOT . $path);
}
setIncludePath('libs/PHPUnit');
echo ini_get('include_path');
require_once('PHPUnit/Framework.php'); ////假设CMS_ROOT='E:/www/'则文件在: 'E:/www/libs/PHPUnit/PHPUnit/Framework.php';
?>
|
并不是所有的配置都可以修改. 手册的php.ini 核心配置选项说明 有几个表,我们看"可修改范围"来知道是否可以在程序中修改各选项. 可修改范围包括:- PHP_INI_USER 可以通过ini_set()来修改,
- PHP_INI_PERDIR 可以在php.ini中修改,如果使用apache,可以在.haccess或 httpd.conf中修改这些值
- PHP_INI_SYSTEM 可在php.ini或httpd.conf中修改这些值
- PHP_INI_ALL 脚本中,php.ini,httpd.conf,.htaccess都可以修改.
|
|