获取目录下文件
1、获取目录下文件,不包括子目录
//打印所有文件名
foreach ($filens as $value) {
echo $value."
";
}
function get_filenamesbydir($dir){
$files = array();
get_allfiles($dir,$files);
return $files;
}
$filenames = get_filenamesbydir("static/image/");
//打印所有文件名,包括路径
foreach ($filenames as $value) {
echo $value."
";
}
计算两个文件之间的相对路径方法
PHP 计算两个文件之间的相对路径方法例如: 文件A 的路径是 /home/web/lib/img/cache.PHP 文件B的路径是 /home/web/api/img/show.PHP 那么,文件A相对于文件B的路径是 ../../lib/img/cache.PHP,即文件B 访问 文件A的相对路径。
function getRelativePath
// 获取相同路径的部分
$intersection = array_intersect_assoc($arr1,$arr2);
$depth = 0;
for($i=0,$len=count($intersection); $i<$len; $i++){
if(!isset($intersection[$i])){
$depth = $i;
break;
}
}
// 将path2的/ 转为 ../,path1获取后面的部分,然后合拼
$tmp = array_merge(array_fill(0,count($arr2)-$depth-1,'..'),array_slice($arr1,$depth));
$relativePath = implode('/',$tmp);
return $relativePath;
}
?>
demo
?>
原文地址:https://www.jb51.cc/php/20538.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。