PHP练习-计算两个文件的相对路径

2020-09-02 19:54 By "Powerless" 2581 0 2

思路分析

从两个路径不同处开始用../填充之后的路径填充完毕后拼接B文件名称(不考虑不同目录)

function filePath($file1,$file2)
{
    $f1 = explode('/',$file1);
    $f2 = explode('/',$file2);
    $path = '';
    foreach ($f1 as $k => $v){
        if(!array_key_exists($k,$f2)){
            $path .= '../';
        }
        if(strspn('.',$v)){
            $path .= end($f2);
        }
    }
    return $path;
}
$f1 = 'a/b/c/d/e.php';
$f2 = 'a/b/c/f.php';
echo filePath($f1,$f2);

输出结果:../f.php

评 论

View in WeChat

Others Discussion

  • 分布式服务限流
    Posted on 2020-02-07 18:57
  • 有状态服务VS无状态服务
    Posted on 2020-02-07 18:18
  • 企业级PAAS云平台几个关键问题和挑战
    Posted on 2019-06-12 18:33
  • MySQL 单库后期分库策略
    Posted on 2019-08-19 14:31
  • Redis七大经典问题
    Posted on 2021-05-27 11:14
  • 关于HTTPS的五大误区
    Posted on 2020-02-02 01:10
  • PHP实现精确发布时间
    Posted on 2018-12-06 21:00
  • 为什么要测量尾部延迟
    Posted on 2020-09-18 10:34

1.248157s