PHP method to get the relative path of a file sample code

怪我咯
Release: 2023-03-13 16:44:02
Original
2267 people have browsed it

The relative path refers to the path relationship with other files (or folders) caused by the path where this file is located. Using relative paths can bring us a lot of convenience. This article mainly introduces the method of obtaining the relative path of the file in PHP. The function of obtaining the relative path of the file is realized through custom function, which has certain reference value. The specific implementation method is as follows:

<?php
$a = &#39;/a/b/c/d/e.php&#39;;
$b = &#39;/a/b/12/34/c.php&#39;;
//../../12/34/c.php
echo getRelativelyPath($a,$b);
//求$b相对于$a的相对路径
function getRelativelyPath($a,$b){ 
$a=explode(&#39;/&#39;,$a);
$b=explode(&#39;/&#39;,$b);
var_dump($a);
//print_r($b);
$c=array_values(array_diff($a,$b));
$d=array_values(array_diff($b,$a));
// var_dump($c);
//var_dump($d);
array_pop($c);
foreach($c as &$v){
$v=&#39;..&#39;;
}
var_dump($c);
var_dump($d);
$arr=array_merge($c,$d);
var_dump($arr);
$str=implode("/",$arr);
echo $str;
}
Copy after login

The above is the detailed content of PHP method to get the relative path of a file sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!