php路径转换的实现方法
php路径转换的方法:1、通过“function sub_rel2abs(string $in_rel, string &$out_abs)”方法将相对路径转换成绝对路径;2、使用“abs2rel”方法将绝对路径转换成相对路径。
推荐:《PHP视频教程》
PHP 相对路径转换为绝对路径 realpath
* 相对路径 -> 绝对路径 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 = []; // divide 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:
/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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8的JIT编译通过将代码经常汇编为机器代码,从而增强了性能,从而使应用程序有益于大量计算并减少执行时间。

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。

本文讨论了PHP中的对称和不对称加密,并比较了它们的适用性,性能和安全差异。对称加密速度更快,适合大量数据,而不对称的键交换则使用。

PHP中准备的陈述通过防止SQL注入并通过编译和重用来提高查询性能,从而增强数据库的安全性和效率。Character计数:159

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手
