自定义函数--获取文件的根目录
来源:互联网 发布:windows sublime git 编辑:程序博客网 时间:2024/05/22 01:52
<?php原始代码: $rootpath = $_SERVER['DOCUMENT_ROOT']; //获取根目录 D:/wamp/www/ $filepath = __FILE__; //获取绝对路径 D:\wamp\www\test\3\newfile.php $len = strlen($rootpath); //获取$rootpath的值的长度 $filepath = substr_replace(__FILE__,'',0, strlen($_SERVER['DOCUMENT_ROOT'])); //substr_replace()替换后的路径包括文件后缀名 $filepath = addCSlashes($filepath,"\\"); //将$filepath的路径中的"\"转义"\\" 做数据库存储 自定义封装函数: function GetRelativePath($filepath){ //定义一个GetRelativePath()函数,函数里填一个路径 的参数 $rootpath = $_SERVER['DOCUMENT_ROOT']; //获取根目录 D:/wamp/www/ $len = strlen($rootpath); //获取$rootpath的值的长度 $temp = substr_replace($filepath,'',0, strlen($_SERVER['DOCUMENT_ROOT'])); //substr_replace()替换后的路径包括文件后缀名 return addCSlashes($temp,"\\"); //返回将$temp的路径中的"\"转义"\\" 做数据库存储 } $filepath = GetRelativePath(__FILE__); //$filepath 调用 GetRelativePath函数(路径)