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

目錄
文法
如何在 PHP 中取得檔案副檔名?
PHP 取得檔案副檔名的範(fàn)例。
範(fàn)例#1
Example #6
Conclusion
首頁 後端開發(fā) php教程 PHP 取得檔案副檔名

PHP 取得檔案副檔名

Aug 29, 2024 pm 01:03 PM
php

PHP 檔案副檔名對(duì)於對(duì)任何上傳的檔案進(jìn)行正確的驗(yàn)證非常有用。從文件名或文件位置獲取擴(kuò)展名非常有用,因?yàn)樗尦淌皆O(shè)計(jì)師知道他們必須上傳或操作僅與 PHP 相關(guān)的文件,而不是任何其他程式語言或具有任何其他副檔名的文件。每當(dāng)需要取得檔案副檔名時(shí),自訂 PHP 都會(huì)發(fā)揮重要作用,因?yàn)檫@些函數(shù)會(huì)傳回所需的檔案副檔名,使用該副檔名可以更新所需的 PHP 相關(guān)檔案。

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

開始您的免費(fèi)軟體開發(fā)課程

網(wǎng)頁開發(fā)、程式語言、軟體測試及其他

文法

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

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

哪裡,

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

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

有很多方法可以在任何程式語言中取得文件副檔名,因?yàn)樗兄蹲R(shí)別內(nèi)容和文件,這與 PHP 相關(guān)以便於操作。因此,PHP 中的檔案副檔名有許多優(yōu)點(diǎn)和缺點(diǎn),在 PHP 中取得副檔名的方法如下:

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

PHP 取得檔案副檔名的範(fàn)例。

以下是下面提到的範(fàn)例

範(fàn)例#1

此程式示範(fàn)了使用 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 取得檔案副檔名的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 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時(shí)區(qū)? 如何設(shè)置PHP時(shí)區(qū)? Jun 25, 2025 am 01:00 AM

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

我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? 我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? 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為擴(kuò)展名,以便服務(wù)器能正確解析。使用標(biāo)準(zhǔn)的標(biāo)籤包裹PHP代碼,可在HTML中任意位置插入動(dòng)態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實(shí)現(xiàn)條件渲染等動(dòng)態(tài)功能。務(wù)必注意服務(wù)器配置及語法正確性,避免因短標(biāo)籤、引號(hào)錯(cuò)誤或遺漏結(jié)束標(biāo)籤導(dǎo)致問題。

編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? 編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? Jun 24, 2025 am 12:53 AM

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

如何使用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