php轉換成絕對路徑的實作方法:1、建立PHP範例檔案;2、透過「function sub_rel2abs(string $in_rel, string &$out_abs) {...}」方法將相對路徑轉換成絕對路徑即可。
本文操作環境:Windows7系統、PHP7.4版、DELL G3電腦
php 怎麼轉換成絕對路徑?
PHP 相對路徑轉換為絕對路徑realpath
* 相對路徑-> 絕對路徑 realpath
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就可以了,為什麼多此一舉?
* 絕對路徑-> 相對路徑
= count($a) || $j >= count($b)) { throw new Exception('$j out of range'); } for ($i = $j; $i >= 0; $i--) { if (strcmp($b[$i], $a[$i])!==0) { return false; } } return true; }; // 找到a,b数组元素相同的下标 while (array_pop($a)) { $i = count($a)-1; if (isset($b[$i])) { if ($sliceEquals($a, $b, $i)) { break; } } array_push($d, ".."); } // 从首个不同元素开始 for ($i+=1; $i < count($b); $i++) { array_push($d, $b[$i]); } return ".".DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $d); }
推薦學習:《PHP視訊教學》
以上是php 怎麼轉換成絕對路徑的詳細內容。更多資訊請關注PHP中文網其他相關文章!