How to get the relative path of a file in PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:06:15
Original
881 people have browsed it

How to get the relative path of a file in PHP,

The example in this article describes how to obtain the relative path of a file in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

<&#63;php
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
//../../12/34/c.php
echo getRelativelyPath($a,$b);
//求$b相对于$a的相对路径
function getRelativelyPath($a,$b){ 
$a=explode('/',$a);
$b=explode('/',$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='..';
}
var_dump($c);
var_dump($d);
$arr=array_merge($c,$d);
var_dump($arr);
$str=implode("/",$arr);
echo $str;
}
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/960707.htmlTechArticleHow to obtain the relative path of a file in PHP. This article describes the method of obtaining the relative path of a file in PHP. Share it with everyone for your reference. The specific implementation method is as follows: php$a = '/a/b/c/d/e...
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!