How to get the relative path of a file using PHP? This article mainly introduces the method of obtaining the relative path of a file in PHP, and implements the function of obtaining the relative path of a file through a custom function. I hope to be helpful.
The example in this article describes how PHP obtains the relative path of a file. Share it with everyone for your reference. The specific implementation method is as follows:
<?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; }
Related recommendations:
php algorithm for splitting arrays without array_chunk ()_PHP tutorial
PHP common sorting algorithm learning
Summary of PHP traversal algorithm
The above is the detailed content of How to get file relative path using PHP. For more information, please follow other related articles on the PHP Chinese website!