[精讚] [會員登入]
7647

[PHP] 取得檔名和路徑:basename, dirname

由絕對路徑取的路徑及檔名的方法

分享此文連結 //n.sfs.tw/10589

分享連結 [PHP] 取得檔名和路徑:basename, dirname @新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 12:23:16 最後編修
2017-01-10 09:04:22 By 張○○
 

自動目錄

由絕對路徑取的路徑及檔名的方法

法一
$path = "/home/httpd/html/index.php";
$file = basename($path);         // index.php
$file = basename($path, ".php"); //  index
$dir = dirname($path);         // /home/httpd/html 注意沒有尾端的 '/'

法二
$path = "/home/httpd/html/index.php";
$fs= pathinfo($path);
print_r($fs);
Array
(
    [dirname] => /home/httpd/html
    [basename] => index.php
    [extension] => php
    [filename] => index
)

法三
$info = new SplFileInfo('/home/httpd/html/index.php');
$fn= $info->getFilename();  //index.php
$dir= $info->getPath(); ///home/httpd/html
$path= $info->getPathname(); //home/httpd/html/index.php

利用類別的方法可以得到更多檔案的資訊,雖然看似麻煩,其實更方便[2]

參考資料

[1] http://stackoverflow.com/questions/1418193/how-to-get-file-name-from-full-path-with-php

[2] http://php.net/manual/en/class.splfileinfo.php

END

你可能感興趣的文章

[Codeingitor4] 使用recaptcha v3前後端認證 為了防止機器人說惡意攻擊,我們引入了 recaptcha,此篇結合 php 框架codeignitor4作前後端認證。

[PHP] 2個程式的觀念 整理2個PHP的程式觀念,參考參考。

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI

[PHP+ci4] codeignitor4+ smarty5 這篇整合 php 的framework codeignitor4 + smarty5。

[PHP] 讀取作業系統程式執行結果 PHP讀取作業系統程式執行結果

設定Google analytics API #3 -- 查詢範例 讓你的網站能夠存取你的Google analytics上面的資料

隨機好文

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

UTF8中文字/全形一覽 快速查詢urf-8的中文字,共計13246中文字(5401常用字+7652罕用字+日文或編號),292全形符號,27半形符號。

兩次使用InnoDB的慘痛經驗 Mysql 的Innodb引擎雖然好用,但是我得說說我兩次的慘痛經驗,這讓我考慮以後可能不會再使用innodb了

[CKeditor4] 修改調色盤 修改ckeditor預設的調色盤

安裝git centos/freebsd/windows安裝git