Php source code calculates the relative paths of two files,

Source: Internet
Author: User

Php source code calculates the relative paths of two files,

<? Php // calculate the relative path of the two files, that is, the relative path of path2 to $ path1 // http://www.manongjc.com/article/1342.htmlfunction get_relative_path ($ path1, $ path2) {$ arr1 = explode (DIRECTORY_SEPARATOR, dirname ($ path1); $ arr2 = explode (DIRECTORY_SEPARATOR, dirname ($ path2); $ length = count ($ arr2); for ($ I = 0; $ I <$ length; $ I ++) {if ($ arr1 [$ I]! = $ Arr2 [$ I]) {// Path Matching break;} if ($ I = 0) {// different return 'in the root directory is not in the same directory';} else if ($ I! = 0 & $ I <$ length) {// $ separator = array_fill (0, ($ length-$ I ),'.. '); return (implode (DIRECTORY_SEPARATOR, array_merge ($ separator, array_slice ($ arr1, $ I);} else if ($ I = $ length) {// The path is exactly the same http://www.manongjc.com/article/1343.html return '. ';}}$ path1 = 'f: \ Apache \ www \ temp \ php_demo \ demo. md '; // Reference Path path1 $ path2 = 'f: \ Apache \ www \ temp \ php_demo \ demo. md '; $ path3 = 'f: \ Apache \ www \ temp \ sd \ ss. d \ Sdsfd '; $ path4 = 'G: \ ffout'; var_dump (get_relative_path ($ path1, $ path2); var_dump (get_relative_path ($ path1, $ path3 )); var_dump (get_relative_path ($ path1, $ path4);?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.