Php code for calculating the relative paths of two files-php Tutorial

Source: Internet
Author: User
Php calculates the implementation code of the relative paths of two files
How to calculate the relative path between two files? Php implementation is very simple. Here we share a piece of code to calculate the relative paths of the two files. if you need it, please refer to it.

Calculate the relative path of the two files. For example, a file is as follows: $ a = "/a/B/c/d/e. php"; $ B = "/a/B/12/34/c. php ". How can we calculate the relative path of B to?

Code:

     $ Counta) {while ($ countb> $ counta) {$ path. = ".. /"; $ countb -- ;}/// find the first public node for ($ I = $ countb-1; $ I >=0 ;) {if ($ patha [$ I]! = $ Pathb [$ I]) {$ path. = ".. /"; $ I --;} else {// determine whether it is the real first public node to prevent the occurrence of sub-directory name duplication. for ($ j = $ I-1, $ flag = 1; $ j> = 0; $ j --) {if ($ patha [$ j] = $ pathb [$ j]) {continue ;} else {$ flag = 0; break ;}} if ($ flag) break; else $ I ++ ;}}for ($ I + = 1; $ I <= $ counta; $ I ++) {$ path. = $ patha [$ I]. "/";} return $ path;} // call example $ a = "/a/c/d/e. php "; $ B ="/a/c. php "; $ path = getRelativePath ($ a, $ B); echo $ path; ?>

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.