[精讚] [會員登入]
7622

[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] IPv6檢查IP是否在某個網段內 mtachcidr6 要檢查IPv6是否在某個IPv6的網段內?

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

[PHP]解決ksort新增的SORT_NATURAL|SORT_FLAG_CASE方法 php>=5.4中ksort函數多了SORT_NATURAL 和 SORT_FLAG_CASE 旗標,對舊版的PHP中要怎麼辦?

[CI3] 使用和修改日曆類別 calendar library CI3有一個日曆圖書館 library,可以快速的建立一個日曆,但是想修改讓他更棒

[PHP] 使用FTP PHP 上使用 FTP 的寫法

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

TFTP Server 安裝及使用 讓設備的網路設定檔或是韌體經由TFTP拷備出來,操作的方法

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..

正則表達式:Email名稱防止連續輸入點(.)及下底線(_) Email的名稱中要允許特殊符號,但又不允許連續出現的正則表達式