php 怎么转换成绝对路径

发布时间:2022-05-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php 怎么转换成绝对路径脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

php转换成绝对路径的实现方法:1、创建一个PHP示例文件;2、通过“function sub_rel2abs(string $in_rel, string &$out_abs) {...}”方法将相对路径转换成绝对路径即可。

php 怎么转换成绝对路径

本文操作环境:Windows7系统、PHP7.4版、DELL G3

php 怎么转换成绝对路径?

PHP 相对路径转换为绝对路径 realpath

* 相对路径 -> 绝对路径 realpath

<?php
/**
 * @param string $in_rel: relative directory
 * @param string $out_abs: absolute directory
 */
define(&#39;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就可以了,为什么多此一举?

* 绝对路径 -> 相对路径

<?php
/**
 * $path相对于$base的相对路径
 * @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数组元素相同的下标
    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 怎么转换成绝对路径全部内容,希望文章能够帮你解决php 怎么转换成绝对路径所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。