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中文網其他相關文章!

本文比較了酸和基本數據庫模型,詳細介紹了它們的特徵和適當的用例。酸優先確定數據完整性和一致性,適合財務和電子商務應用程序,而基礎則側重於可用性和

本文討論了確保PHP文件上傳的確保,以防止諸如代碼注入之類的漏洞。它專注於文件類型驗證,安全存儲和錯誤處理以增強應用程序安全性。

本文討論了在PHP中實施API速率限制的策略,包括諸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之類的庫。它還涵蓋監視,動態調整速率限制和手

本文討論了使用password_hash和pyspasswify在PHP中使用密碼的好處。主要論點是,這些功能通過自動鹽,強大的哈希算法和SECH來增強密碼保護

本文討論了OWASP在PHP和緩解策略中的十大漏洞。關鍵問題包括注射,驗證損壞和XSS,並提供用於監視和保護PHP應用程序的推薦工具。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器