<?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函数(路径)
PHP 自定义函数
最新推荐文章于 2022-03-16 18:09:31 发布