>  기사  >  php教程  >  PHP 소스 코드는 두 파일의 상대 경로를 계산합니다.

PHP 소스 코드는 두 파일의 상대 경로를 계산합니다.

WBOY
WBOY원래의
2016-10-28 15:03:261374검색
<?<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> < <span style="color: #800080">$length</span>; <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> < <span style="color: #800080">$length</span>){        <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>?>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.