How to convert php to absolute path

藏色散人
Release: 2023-03-14 09:26:01
Original
2750 people have browsed it

How to convert php into an absolute path: 1. Create a PHP sample file; 2. Convert the relative path through the "function sub_rel2abs(string $in_rel, string &$out_abs) {...}" method Just make it an absolute path.

How to convert php to absolute path

The operating environment of this article: Windows 7 system, PHP version 7.4, DELL G3 computer

How to convert php to an absolute path?

PHP Convert relative path to absolute path realpath

* Relative path-> Absolute path realpath

<?php
/**
 * @param string $in_rel: relative directory
 * @param string $out_abs: absolute directory
 */
define(&#39;PATH_MAX&#39;, 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);
Copy after login

output:

0
/Users/Mch/eclipse-workspace/blog.zip
Copy after login

Directly here Realpath is enough, why bother?

* Absolute path-> Relative path

<?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(&#39;$j out of range&#39;);
        }
        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);
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php to absolute path. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!