国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
語法
如何在 PHP 中獲取文件擴展名?
PHP 獲取文件擴展名的示例。
示例#1
Example #6
Conclusion
首頁 后端開發(fā) php教程 PHP 獲取文件擴展名

PHP 獲取文件擴展名

Aug 29, 2024 pm 01:03 PM
php

PHP 文件擴展名對于對任何上傳的文件進行正確的驗證非常有用。從文件名或文件位置獲取擴展名非常有用,因為它讓程序員知道他們必須上傳或操作僅與 PHP 相關(guān)的文件,而不是任何其他編程語言或具有任何其他擴展名的文件。每當需要獲取文件擴展名時,自定義 PHP 都會發(fā)揮重要作用,因為這些函數(shù)會返回所需的文件擴展名,使用該擴展名可以更新所需的 PHP 相關(guān)文件。

廣告 該類別中的熱門課程 PHP 開發(fā)人員 - 專業(yè)化 | 8 門課程系列 | 3次模擬測試

開始您的免費軟件開發(fā)課程

網(wǎng)絡(luò)開發(fā)、編程語言、軟件測試及其他

語法

每種編程語言都有文件擴展名,PHP 文件也有,表示為:

public SplFileInfo::getExtension() : string, filename etc.

哪里,

  • public:它是一個訪問修飾符,用于證明程序的作用域是公共的,任何人都可以訪問。
  • SplFileInfo:包含文件名和內(nèi)容,用于獲取與文件相關(guān)的信息。
  • getExtension():這是PHP中的函數(shù),用于獲取與PHP相關(guān)的擴展。
  • 字符串:字符串是作為 getExtension() 函數(shù)一部分提供的輸入類型。

如何在 PHP 中獲取文件擴展名?

有很多方法可以在任何編程語言中獲取文件擴展名,因為它有助于識別內(nèi)容和文件,這與 PHP 相關(guān)以便于操作。因此,PHP 中的文件擴展名有很多優(yōu)點和缺點,在 PHP 中獲取擴展名的方法如下:

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

PHP 獲取文件擴展名的示例。

以下是下面提到的示例

示例#1

此程序演示了使用 SplFileInfo 作為其 inbuild 函數(shù)的 getExtension 函數(shù),該函數(shù)允許傳遞帶有擴展名的任何字符串以獲得 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 獲取文件擴展名的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔相應(yīng)法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實踐? 我如何了解最新的PHP開發(fā)和最佳實踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什么是PHP,為什么它用于Web開發(fā)? 什么是PHP,為什么它用于Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時區(qū)? 如何設(shè)置PHP時區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗證PHP中的用戶輸入以確保其符合某些標準? 我如何驗證PHP中的用戶輸入以確保其符合某些標準? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什么是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什么是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴展名,以便服務(wù)器能正確解析。使用標準的標簽包裹PHP代碼,可在HTML中任意位置插入動態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實現(xiàn)條件渲染等動態(tài)功能。務(wù)必注意服務(wù)器配置及語法正確性,避免因短標簽、引號錯誤或遺漏結(jié)束標簽導致問題。

編寫清潔和可維護的PHP代碼的最佳實踐是什么? 編寫清潔和可維護的PHP代碼的最佳實踐是什么? Jun 24, 2025 am 12:53 AM

寫干凈、易維護的PHP代碼關(guān)鍵在于清晰命名、遵循標準、合理結(jié)構(gòu)、善用注釋和可測試性。1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標準統(tǒng)一代碼風格;3.按職責拆分代碼結(jié)構(gòu),使用MVC或Laravel式目錄組織;4.避免面條式代碼,將邏輯拆分為單一職責的小函數(shù);5.在關(guān)鍵處添加注釋并撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測試性,采用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質(zhì)量、協(xié)作效率和后期維護便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles