DIRECTORY_SEPARATOR的作用

本文详细介绍了PHP内置常量DIRECTORY_SEPARATOR的作用及使用方法,该常量能够根据操作系统返回相应的路径分隔符,有效解决跨平台路径拼接问题。

DIRECTORY_SEPARATOR是php的内部常量,用于显示系统分隔符的命令,不需要任何定义与包含即可直接使用。

在windows下路径分隔符是/(当然/在部分系统上也是可以正常运行的),在linux上路径的分隔符是/,这就导致了一个问题,比如开发机器是windows,有一个图片上传程序,调试机器上指定的上传文件保存目录是:

define('ROOT', dirname(__FILE__)."/upload");

在本地调试都很正常,但是上传到linux服务器的时候会发现会出错。于是php引入了DIRECTORY_SEPARATOR变量,可以将上面的写法改写为:

define('ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR."upload");

DIRECTORY_SEPARATOR是一个返回跟操作系统相关的路径分隔符的php内置命令,在windows上返回/,而在linux或者类unix上返回/,就是这么个区别,通常在定义包含文件路径或者上传保存目录的时候会用到。

例如:

const DIR_SEP = DIRECTORY_SEPARATOR;// 路径分割 win下/ linux下/ private function __construct() { $this->_options = array( 'template_dir' => 'templates' . self::DIR_SEP, //模板文件所在目录 'cache_dir' => 'templates' . self::DIR_SEP . 'cache' . self::DIR_SEP, //缓存文件存放目录 'auto_update' => false, //当模板文件改动时是否重新生成缓存 'cache_lifetime' => 0, //缓存生命周期(分钟),为 0 表示永久 'suffix' => '.html' //模板文件后缀 ); }

<?php /* '软件名称:漫城CMS(Mccms) '官方网站:http://www.mccms.cn/ '软件作者:桂林崇胜网络科技有限公司(By:烟雨江南) '-------------------------------------------------------- 'Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) '遵循Apache2开源协议发布,并提供免费使用。 '-------------------------------------------------------- */ //默认时区 date_default_timezone_set("Asia/Shanghai"); define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production'); switch (ENVIRONMENT){ case 'development': error_reporting(-1); ini_set('display_errors', 1); break; case 'testing': case 'production': ini_set('display_errors', 0); if (version_compare(PHP_VERSION, '5.3', '>=')){ error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); }else{ error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); } break; default: header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'The application environment is not set correctly.'; exit(1); } $application_folder = 'sys/apps'; $system_path = 'sys/system'; $view_folder = 'template'; if(defined('STDIN')) chdir(dirname(__FILE__)); if(($_temp = realpath($system_path)) !== FALSE){ $system_path = $_temp.'/'; }else{ $system_path = rtrim($system_path, '/').'/'; } if(!is_dir($system_path)){ header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME); exit(3); } if(!defined('SELF')) { define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); } define('BASEPATH', str_replace('\\', '/', $system_path)); if(!defined('FCPATH')) define('FCPATH', str_replace('\\', '/', dirname(__FILE__).'/')); define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/')); define('MCCMSPATH', str_replace('\\', '/', dirname(BASEPATH).DIRECTORY_SEPARATOR)); if(is_dir($application_folder)){ if(($_temp = realpath($application_folder)) !== FALSE){ $application_folder = $_temp; } define('APPPATH', $application_folder.DIRECTORY_SEPARATOR); }else{ header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF; exit(3); } if(($_temp = realpath($view_folder)) !== FALSE){ $view_folder = $_temp.DIRECTORY_SEPARATOR; }else{ $view_folder = rtrim($view_folder, '/\\').DIRECTORY_SEPARATOR; } if(!is_dir($view_folder)){ header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME); exit(3); } define('VIEWPATH', $view_folder); require_once MCCMSPATH.'libs/sys.php'; require_once BASEPATH.'core/CodeIgniter.php';
最新发布
08-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值