Rumah > php教程 > php手册 > php源码之计算两个文件的相对路径

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

WBOY
Lepaskan: 2016-10-28 15:03:26
asal
1390 orang telah melayarinya
<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>?>
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan