溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何轉(zhuǎn)換成絕對(duì)路徑

發(fā)布時(shí)間:2021-12-02 10:03:16 來源:億速云 閱讀:160 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹php如何轉(zhuǎn)換成絕對(duì)路徑,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

php轉(zhuǎn)換成絕對(duì)路徑的實(shí)現(xiàn)方法:1、創(chuàng)建一個(gè)PHP示例文件;2、通過“function sub_rel2abs(string $in_rel, string &$out_abs) {...}”方法將相對(duì)路徑轉(zhuǎn)換成絕對(duì)路徑即可。

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.4版、DELL G3電腦

PHP 相對(duì)路徑轉(zhuǎn)換為絕對(duì)路徑 realpath

* 相對(duì)路徑 -> 絕對(duì)路徑 realpath

<?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_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就可以了,為什么多此一舉?

*  絕對(duì)路徑 -> 相對(duì)路徑

<?php
/**
 * $path相對(duì)于$base的相對(duì)路徑
 * @param string $base
 * @param string $path
 */
function abs2rel(string $base, string $path) {
    if (is_dir($base)) {
        $base = rtrim($base, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ".";
    }

    $a = explode(DIRECTORY_SEPARATOR, $base);
    $b = explode(DIRECTORY_SEPARATOR, $path);
 
    $d = [];   // $path push
    $i = count($a)-1;
 
    $sliceEquals = function($a, $b, $j) {
        if ($j >= 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數(shù)組元素相同的下標(biāo)
    while (array_pop($a)) {
        $i = count($a)-1;
        if (isset($b[$i])) {
            if ($sliceEquals($a, $b, $i)) {
                break;
            }
        }
        array_push($d, "..");
    }
    // 從首個(gè)不同元素開始
    for ($i+=1; $i < count($b); $i++) {
        array_push($d, $b[$i]);
    }
    return ".".DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $d);
}


以上是“php如何轉(zhuǎn)換成絕對(duì)路徑”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI