在PHP 中從路徑中提取檔案名稱
在PHP 中,經常需要從完整路徑中擷取檔案名,特別是當擷取檔案名使用目錄或操作檔案系統。讓我們探討如何利用 PHP 的強大功能來實現這一點。
問題:從完整路徑擷取檔案名稱
假設我們有以下完整路徑:
F:Program FilesSSH Communications SecuritySSH Secure ShellOutput.map
F:Program FilesSSH Communications SecuritySSH Secure ShellOutput.mapF:Program FilesSSH Communications SecuritySSH Secure ShellOutput.map
F:Program FilesSSH Communications SecuritySSH Secure ShellOutput.map我們希望提取檔案名,即「Output.map」。
解決方案:使用basename() 函數
PHP basename() 函數提供了一種簡潔便捷的方法從給定的檔案名稱中擷取檔案名稱路徑。
basename(path, suffix)語法:
路徑:
<?php $path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map"; $file_name = basename($path); echo $file_name; // Output: Output.map ?>路徑:
後綴(可選):
<?php $path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map"; $file_name_without_extension = basename($path, ".map"); echo $file_name_without_extension; // Output: Output ?>如果提供,則從檔案名稱中刪除指定的後綴。 範例:從我們的範例中提取檔案名稱路徑:刪除檔案副檔名:後綴參數可用於從擷取的檔案名稱中刪除任何檔案副檔名。例如,要從「Output.map」中提取「Output」:透過利用PHP 的basename() 函數,開發人員可以輕鬆靈活地從完整路徑中高效地提取文件名。此功能在各種文件管理和操作場景中特別有用。
以上是如何從 PHP 的完整路徑中提取檔案名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!