PHP通过realpath将相对路径转换为绝对路径的代码实例

PHP通过realpath将相对路径转换为绝对路径的代码实例

内容导读

收集整理的这篇技术教程文章主要介绍了PHP通过realpath将相对路径转换为绝对路径的代码实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1864字,纯文字阅读大概需要3分钟

内容图文

本篇文章给大家带来的内容是关于PHP通过 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就可以了,为什么多此一举?

相关文章推荐:

PHP中如何将session存入数据库并使用(附代码)

PHP如何访问URL?php访问URL的方法总结(代码)

以上就是PHP通过 realpath将相对路径转换为绝对路径的代码实例的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的PHP通过realpath将相对路径转换为绝对路径的代码实例全部内容,希望文章能够帮你解决PHP通过realpath将相对路径转换为绝对路径的代码实例所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com