Home > Backend Development > PHP Tutorial > PHP获取文件相对路径的方法_PHP

PHP获取文件相对路径的方法_PHP

WBOY
Release: 2016-05-31 13:16:47
Original
889 people have browsed it

本文实例讲述了PHP获取文件相对路径的方法。分享给大家供大家参考。具体实现方法如下:

<&#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

希望本文所述对大家的php程序设计有所帮助。

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