PHP 取得檔案副檔名

WBOY
發布: 2024-08-29 13:03:04
原創
432 人瀏覽過

PHP 檔案副檔名對於對任何上傳的檔案進行正確的驗證非常有用。從文件名或文件位置獲取擴展名非常有用,因為它讓程式設計師知道他們必須上傳或操作僅與 PHP 相關的文件,而不是任何其他程式語言或具有任何其他副檔名的文件。每當需要取得檔案副檔名時,自訂 PHP 都會發揮重要作用,因為這些函數會傳回所需的檔案副檔名,使用該副檔名可以更新所需的 PHP 相關檔案。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

每種程式語言都有檔案副檔名,PHP 檔案也有,表示為:

public SplFileInfo::getExtension() : string, filename etc.
登入後複製

哪裡,

  • public:它是一個存取修飾符,用來證明程式的作用域是公共的,任何人都可以存取。
  • SplFileInfo:包含檔案名稱和內容,用於取得與檔案相關的資訊。
  • getExtension():這是PHP中的函數,用來取得與PHP相關的擴充。
  • 字串:字串是作為 getExtension() 函數一部分提供的輸入類型。

如何在 PHP 中取得檔案副檔名?

有很多方法可以在任何程式語言中取得文件副檔名,因為它有助於識別內容和文件,這與 PHP 相關以便於操作。因此,PHP 中的檔案副檔名有許多優點和缺點,在 PHP 中取得副檔名的方法如下:

  • 一些自訂函數可用於將參數作為擴充功能返回,大多數 PHP 版本都支援這些函數,包括像 substr() 這樣的函數,該函數廣泛用於傳回字串的一部分。
  • 用於返回擴展名作為字串一部分的另一個函數包括 strrchr(),它用於查找字串在另一個字串中最後一次出現的位置,或者可以說是嵌套字串。
  • PHP pathinfo() 函數是用於返回有關PHP 的文件信息的另一種簡單方法,用於獲取所有必要且重要的信息,例如文件所在的路徑,後跟目錄名、基本文件名稱,文件的擴展名。
  • 與 SplFileInfo 一起使用時,取得檔案副檔名也可以透過考慮所需的字串來傳回預期的檔案副檔名。
  • PHP 程式設計師忘記放置擴展的情況有很多;因此可以說PHP 程式中不存在擴展,在這種情況下,將很難使用所有必要的資訊來處理文件,從而使程式設計師處理空字串,因為傳回類型將為空字串。
  • 有很多方法可以確定副檔名或取得檔案副檔名;在這種情況下,可能有機會在pathinfo() 函數中使用多個點,它將追蹤嵌套路徑,然後傳回與預期相同的結果,而不會在最後一種檔案副檔名上遇到太多麻煩。
  • 在PHP 中使用pathinfo() 函數時存在一個微小的差異,在實作PHP 程式碼時,如果使用null 且未定義副檔名,則當使用前一個函數檢索字串時,它將傳回null另一方面,當沒有提供擴展名時,當某些不匹配或預設值被輸入時,就會出現轉折,它的行為有點類似於空字串,而不提供任何相關或必需的資訊。
  • 因此,這些函數對於 PHP 開發人員保持元資料和其他與檔案相關的操作正確執行非常有用。
  • 許多工作都是為了在 PHP 中取得檔案副檔名,但所描述的方法很容易理解和執行。

PHP 取得檔案副檔名的範例。

以下是下面提到的範例

範例#1

此程式示範了使用 SplFileInfo 作為其 inbuild 函數的 getExtension 函數,該函數允許傳遞帶有副檔名的任何字串以獲得 PHP 程式碼庫的支持,如輸出所示。

代碼:

<?php
$in_1 = new SplFileInfo('foam.txt');
var_dump($in_1->getExtension());
$in_1 = new SplFileInfo('import_ant.tar.gz');
var_dump($in_1->getExtension());
$in_1 = new SplFileInfo('image_with.jpeg');
var_dump($in_1->getExtension());
?>
登入後複製

輸出:

PHP 取得檔案副檔名

Example #2

This program demonstrates the pathinfo, which tells about the given file’s information and can be used to get the directory name, base name, file name, and extension. It will return the text file as shown in the output.

Code:

<?php
$fil_nm = 'directory_1/anu.txt';
$extnsn = pathinfo($fil_nm, PATHINFO_EXTENSION);
var_dump($extnsn);
?>
登入後複製

Output:

PHP 取得檔案副檔名

Example #3

This program demonstrates the approach to get the file extension if in a file with multiple periods or dots exists then also pathinfo will work as shown in the output.

Code:

<?php
$fl_nm = 'fldr/exmpl.txt.jpeg.tar.gz';
$extnsn = pathinfo($fl_nm, PATHINFO_EXTENSION);
var_dump($extnsn);
?>
登入後複製

Output:

PHP 取得檔案副檔名

Example #4

This program demonstrates the custom function which is used for returning substring by calling the function wherever required, as shown in the output.

Code:

<?php
function get_fl_extnsn($fname) {
return substr(strrchr($fname,'.'),1);
}
echo get_fl_extnsn('extension.jpg.txt');
?>
登入後複製

Output:

PHP 取得檔案副檔名

Example #5

This program demonstrates the output if there is no extension if an empty string with pathinfo() function will return as shown.

Code:

<?php
$fl_pth = 'path/to_1/file_n/';
$extn = pathinfo($fl_pth, PATHINFO_EXTENSION);
var_dump($extn);
?>
登入後複製

Output:

PHP 取得檔案副檔名

Example #6

This program demonstrates the pathinfo regarding the file name with an extension which is being included and is manipulated as per the requirement as shown in the output. It can support a multi dot file structure for getting the extension.

Code:

<?php
$pth_pp = pathinfo('include/bin.include.php');
echo $pth_pp['extension'], "\n";
echo $pth_pp['filename'], "\n";
?>
登入後複製

Output:

PHP 取得檔案副檔名

Conclusion

PHP get file extension like other programming languages is used for identifying the type of PHP file that will get uploaded and manipulated by giving some must information like a directory, extension, file name, basename. It is advantageous when some troubleshooting is required from getting out of a stuck situation.

以上是PHP 取得檔案副檔名的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!