相對路徑 - 絕對路徑:
?php * @param string $in_rel: relative directory * @param string $out_abs: absolute directorydefine( PATH_MAX , 255);function sub_rel2abs(string $in_rel, string $out_abs) { $i_rtn = 0; // return html' target='_blank'>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_fpos)); $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
這里直接realpath就可以了,為什么多此一舉?
相關文章推薦:
PHP中如何將session存入數據庫并使用(附代碼)
PHP如何訪問URL?php訪問URL的方法總結(代碼)
以上就是PHP通過 realpath將相對路徑轉換為絕對路徑的代碼實例的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答