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?