[精讚] [會員登入]
7307

[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

你可能感興趣的文章

[PHP] 將UTF8中文字轉成10進位或16進位數值 原本為了處理 preg_match 中文字的問題[2],用php把中文字轉換成10進位和6進位的數值編碼

[PHP] CodeIgniter 3+pure+smarty安裝及環境設置2/2 PHP framework CodeIgniter 3+ pure CSS +smarty Template Engine的整合#2

[PHP] 陣列排序 sort, ksort, asort, usort... 簡單的記錄PHP的陣列排序,因為常常會用到。

[PHP] 移除檔案的UTF8 BOM 移除檔案的UTF8 BOM

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

[CodeIgniter 3] COOKIE的使用 PHP CodeIgniter 3 中COOKIE的使用超簡單

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

[JAVA] JWS, JWT, JWE, JOSE是什麼? [JAVA] JWS, JWT, JWE, JOSE是什麼?非常的複雜,儘量來搞清楚..

好用的3+2碼郵遞區號查詢系統推薦 網路上找到用地址輸入判斷3+2碼郵遞區號的辨識率不高,除了這個網站…

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔