Code instance where PHP converts a relative path to an absolute path via Realpath

Source: Internet
Author: User
This article brings you the content is about PHP through the Realpath to convert the relative path to absolute path code example, there is a certain reference value, the need for friends can refer to, I hope to help you.

Absolute path, relative path:

<?php/** * @param string $in _rel:relative directory * @param string $out _abs:absolute directory */define (' Path_max ',  255); function Sub_rel2abs (String $in _rel, String & $out _abs) {$i _rtn = 0;  return value $ss _rel = "";    For relative path build $st _fpos = 0;   Front Separator index $SV _path = [];    Pide path to array $st _pos = Strpos ($in _rel, directory_separator);    $npos = 0; while ($npos! = $st _pos) {if ($st _pos! = 0) {Array_push ($sv _path, substr ($in _rel, $st _fpos, $st _pos-        $st _fpos));   }//next ... $st _fpos = $st _pos;            Set current pos-to-last POS $st _pos++;  From next index $st _pos = Strpos ($in _rel, Directory_separator, $st _pos); Next separator Index}//while ($npos! = $st _pos)//FINAL separator Array_push ($sv _path, substr ($in _rel, $st _f     POS));    $LPC = 0;    Loop count $i _max = count ($sv _path); while ($LPC < $i _max && 0 = = = $i _rtn) {$ss_rel. = $sv _path[$LPC];//relative path = relative path $c _abs = Realpath ($ss _rel);        if ($c _abs = = = False) {$i _rtn =-1;            } else {$ss _rel = $c _abs;        $i _rtn = 0;    } $LPC + +;  }//while (count ($sv _path) >0)//Normal ending if (0=== $i _rtn) {$out _abs = $ss _rel; Set converted path} return $i _rtn;} Test$indir = "/users/mch/code/php/directory"; Is_dir ($inDir) | | mkdir ($inDir, 0777, true); $WD = __dir__;chdir ($inDir); $out = ""; Echo sub_rel2abs (".. /.. /.. /eclipse-workspace/blog.zip ", $out). Php_eol;echo $out. Php_eol; ChDir ($WD); @rmdir ($inDir);? >

Output

0/users/mch/eclipse-workspace/blog.zip

Here directly realpath can, why superfluous?

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.