Home > Backend Development > PHP Tutorial > How to get the relative path of a file in PHP_PHP tutorial

How to get the relative path of a file in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:06:00
Original
824 people have browsed it

How to obtain the relative path of a file in PHP

This article mainly introduces the method of obtaining the relative path of a file in PHP. The function of obtaining the relative path of a file is realized through a custom function, which has certain For reference value, friends in need can refer to it

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

$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;

}

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23 24 25 26
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/961078.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/961078.htmlTechArticleHow to get the relative path of a file in PHP. This article mainly introduces the method of getting the relative path of a file in PHP, through customization The function realizes the function of obtaining the relative path of the file, and has certain reference...
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