php求兩個檔案相對路徑_PHP教程

來源:互聯網
上載者:User
//用php求兩個檔案的相對路徑
function compara_path($path_a, $path_b) {
//切割路徑.
$array_a =explode('/', $path_a);
$array_b =explode('/', $path_b);
//從數組中刪除最後的檔案,剩下的全為檔案夾名稱.
$file_a =array_pop($array_a); //array_pop() 彈出並返回 array 數組的最後一個單元
$file_b =array_pop($array_b);
//子目錄個數.
$a_len =count($array_a);
$b_len =count($array_b);
//迴圈求出第幾個目錄不同.
for ( $i =0; $i < $a_len; $i++ ) {
if ($array_a[$i] != $array_b[$i] ) {
break;
}
}
//求出相對路徑.
$com_path ="";
for ( $j =0; $j < $a_len - $i; $j++ ) {
$com_path .='../';
}
for ( $i; $i< $b_len; $i++ ) {
$com_path .=$array_b[$i] . '/';
}
$com_path .=$file_b;
return $com_path;
}
$path_a = "a/b/c/d/e/f.php";
$path_b = "a/b/z/x/y.php";
echo compara_path($path_a, $path_b);

摘自:小囧的部落格

http://www.bkjia.com/PHPjc/478585.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478585.htmlTechArticle?php //用php求兩個檔案的相對路徑 function compara_path($path_a, $path_b) { //切割路徑. $array_a =explode(/, $path_a); $array_b =explode(/, $path_b); //從數組中刪除...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.