php dirname、basename、pathinfo 解析目錄路徑

來源:互聯網
上載者:User
本文章詳細的介紹了關於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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.