本文章詳細的介紹了關於php dirname、basename、pathinfo 解析目錄路徑,有需要學習的朋友可參考一下。
1:string basename(string path[,string suffix]);
給出一個包含有指向一個檔案的全路徑的字串,本函數返回基本的檔案名稱。如果檔案名稱是以suffix結束的,那這一部分也會被去掉。
在Windows中,斜線(/)和反斜線()都可以用作路徑分隔字元。在其它環境下是斜線(/)。
例子1.basename()例子,代碼如下
<?php$path=網頁製作教程http://www.bKjia.c0m,請保留此標記"/home/httpd/html/index.php";$file=basename($path); //$fileissetto"index.php"$file=basename($path,".php");//$fileissetto"index"?>
參數是是一個檔案路徑的字串,返回去掉檔案名稱後的目錄
2:string dirname(string path);
__FILE__的路徑是當前代碼所在檔案
dirname(dirname(__FILE__));得到的是檔案上一層目錄名
dirname(__FILE__);得到的是檔案所在層目錄名
例子,代碼如下
<?phpecho dirname("c:/testweb/home.php");echo dirname("/testweb/home.php");?>
輸出:
c:/testweb/testweb
參數是一個檔案路徑的字串,返回一個包含目錄名、檔案名稱和副檔名三個部分的數組,分別通過dirname、basename、extension來引用3:array pathinfo($path);
例子 1
<?phpprint_r(pathinfo("/testweb/test.txt"));?>
輸出結果:
Array([dirname] => /testweb[basename] => test.txt[extension] => txt)
例子 2
<?phpprint_r(pathinfo("/testweb/test.txt",PATHINFO_BASENAME));?>
輸出結果:
test.txt