[精讚] [會員登入]
7308

[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] 移除陣列或字串中的重覆元素 移除陣列或字串中的重覆元素

PHP程式經驗 #1 -- 靜態的物件比動態物件快 驗證 1. 使用動態物件函式 > 使用靜態物件函式。 2. 使用有宣告的靜態物件函式 > 使用動態無宣告的靜

[PHP] UTF8中取出字串中特定的字數 要將字串,例如資料庫取出的TEXT,取出特定的字數

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

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

外匯課程經驗 有天遇到某個朋友的正妹朋友,說她有個很棒的外匯投資課程,要我們去聽,只收場地費200大洋。她又說續效最差一個月也有1%,誰還在苦哈哈上班?

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper

「許功蓋」的字以及源由 有玩過電腦一段時間的人,都聽過這個人(有一天我才發現7年級的竟然都不認識這個

維修海棉拖把頭 這類型的海棉拖把很好用,可是這近發現海棉頭越來越不耐用,也許是錯覺,以往都能用個三四個月,現在二個月就差不多掉下來。 這