首页 > php教程 > php手册 > 正文

php源码之计算两个文件的相对路径

WBOY
发布: 2016-10-28 15:03:26
原创
1376 人浏览过
<span style="color: #000000">php
</span><span style="color: #008000">//</span><span style="color: #008000">计算出两个文件的相对路径即path2相对于$path1的相对路径
// http://www.manongjc.com/article/1342.html</span>
<span style="color: #0000ff">function</span> get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path2</span><span style="color: #000000">){
    </span><span style="color: #800080">$arr1</span> = <span style="color: #008080">explode</span>(DIRECTORY_SEPARATOR,<span style="color: #008080">dirname</span>(<span style="color: #800080">$path1</span><span style="color: #000000">));
    </span><span style="color: #800080">$arr2</span> = <span style="color: #008080">explode</span>(DIRECTORY_SEPARATOR,<span style="color: #008080">dirname</span>(<span style="color: #800080">$path2</span><span style="color: #000000">));
    </span><span style="color: #800080">$length</span> = <span style="color: #008080">count</span>(<span style="color: #800080">$arr2</span><span style="color: #000000">);
    </span><span style="color: #0000ff">for</span>(<span style="color: #800080">$i</span> = 0; <span style="color: #800080">$i</span> $length; <span style="color: #800080">$i</span>++<span style="color: #000000">){
        </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$arr1</span>[<span style="color: #800080">$i</span>] != <span style="color: #800080">$arr2</span>[<span style="color: #800080">$i</span>]){ <span style="color: #008000">//</span><span style="color: #008000">进行路径匹配</span>
            <span style="color: #0000ff">break</span><span style="color: #000000">;
        }
    }
    </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> == 0){                    <span style="color: #008000">//</span><span style="color: #008000">根目录不同</span>
        <span style="color: #0000ff">return</span> '不在同一个目录中'<span style="color: #000000">;
    }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> != 0 && <span style="color: #800080">$i</span> $length){        <span style="color: #008000">//</span><span style="color: #008000">在同一个个目录下</span>
        <span style="color: #800080">$separator</span> = <span style="color: #008080">array_fill</span>(0, (<span style="color: #800080">$length</span>-<span style="color: #800080">$i</span>), '..'<span style="color: #000000">);
        </span><span style="color: #0000ff">return</span>(<span style="color: #008080">implode</span>(DIRECTORY_SEPARATOR, <span style="color: #008080">array_merge</span>(<span style="color: #800080">$separator</span>, <span style="color: #008080">array_slice</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$i</span><span style="color: #000000">))));
    }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> == <span style="color: #800080">$length</span>){                <span style="color: #008000">//</span><span style="color: #008000">路径完全相同 http://www.manongjc.com/article/1343.html</span>
        <span style="color: #0000ff">return</span> '.'<span style="color: #000000">;
    }
}
</span><span style="color: #800080">$path1</span> = 'F:\Apache\www\temp\php_demo\demo.md'; <span style="color: #008000">//</span><span style="color: #008000">参照路径path1</span>
<span style="color: #800080">$path2</span> = 'F:\Apache\www\temp\php_demo\demo.md'<span style="color: #000000">;
</span><span style="color: #800080">$path3</span> = 'F:\Apache\www\temp\sd\ss.d\sdsfd'<span style="color: #000000">;
</span><span style="color: #800080">$path4</span> = 'G:\FFOutput'<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path2</span><span style="color: #000000">));
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path3</span><span style="color: #000000">));
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path4</span><span style="color: #000000">));
</span>?>
登录后复制

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板