Home > Backend Development > PHP Tutorial > How to calculate the relative path between two files through php

How to calculate the relative path between two files through php

jacklove
Release: 2023-03-31 08:54:01
Original
2279 people have browsed it

php method to calculate the relative path between two files

For example:

File The path of file A is /home/web/lib/img/cache.php

The path of file B is /home/web/api/img /show.php

Then, the path of file A relative to file B is ../../lib/img/cache.php, That is, file B accesses the relative path of file A.

function getRelativePath

<?php
/** 计算path1 相对于 path2 的路径,即在path2引用paht1的相对路径
* @param  String $path1
* @param  String $path2
* @return String
*/
function getRelativePath($path1, $path2){
    $arr1 = explode(&#39;/&#39;, $path1);
    $arr2 = explode(&#39;/&#39;, $path2);
    // 获取相同路径的部分
    $intersection = array_intersect_assoc($arr1, $arr2);
    $depth = 0;
    for($i=0,$len=count($intersection); $i<$len; $i++){
        $depth = $i;
        if(!isset($intersection[$i])){
            break;
        }
    }
    // 前面全部匹配
    if($i==count($intersection)){
        $depth ++;
    }
    // 将path2的/ 转为 ../,path1获取后面的部分,然后合拼
    
    // 计算前缀
    if(count($arr2)-$depth-1>0){
        $prefix = array_fill(0, count($arr2)-$depth-1, &#39;..&#39;);
    }else{
        $prefix = array(&#39;.&#39;);
    }
    $tmp = array_merge($prefix, array_slice($arr1, $depth));
    $relativePath = implode(&#39;/&#39;, $tmp);
    return $relativePath;
}
?>
Copy after login

demo

<?php
$path1 = &#39;/home/web/lib/img/cache.php&#39;;
$path2 = &#39;/home/show.php&#39;;
echo getRelativePath($path1, $path2).&#39;<br>&#39;; // ./web/lib/img/cache.php
$path1 = &#39;/home/web/lib/img/cache.php&#39;;
$path2 = &#39;/home/web/api/show.php&#39;;
echo getRelativePath($path1, $path2).&#39;<br>&#39;; // ../lib/img/cache.php
$path1 = &#39;/home/web/lib/img/cache.php&#39;;  
$path2 = &#39;/home/web/api/img/show.php&#39;;  
echo getRelativePath($path1, $path2).&#39;<br>&#39;; // ../../lib/img/cache.php
$path1 = &#39;/home/web/lib/img/cache.php&#39;;
$path2 = &#39;/xhome/web/show.php&#39;;
echo getRelativePath($path1, $path2).&#39;<br>&#39;; // ../../home/web/lib/img/cache.php
?>
Copy after login

This article explains how to calculate the path between two files through PHP Relative path method, please pay attention to php Chinese website for more related content.

Related recommendations:

Explain how PHP obtains relevant content on a specified date

Detailed explanation of how PHP generates a unique RequestID class

How to check the database table capacity through MySQL

The above is the detailed content of How to calculate the relative path between two files through php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template