Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get file extension in PHP

Shulou Source: shulou.com Published: 2022-06-02 08:40:43 09月24日 Update

I believe many inexperienced people don't know what to do about how to get the file extension in PHP. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

/ / method 1:

Function extend_1 ($file_name)

{

$retval= ""

$pt=strrpos ($file_name, ".")

If ($pt) $retval=substr ($file_name, $pt+1, strlen ($file_name)-$pt)

Return ($retval)

}

/ / method 2

Php code

Function extend_2 ($file_name)

{

$extend = pathinfo ($file_name)

$extend = strtolower ($extend ["extension"])

Return $extend

}

Function extend_2 ($file_name) {$extend = pathinfo ($file_name); $extend = strtolower ($extend ["extension"]); return $extend;}

/ / method 3

Php code

Function extend_3 ($file_name)

{

$extend = explode (".", $file_name)

$va=count ($extend)-1

Return $extend [$va]

}

Function extend_3 ($file_name) {$extend = explode (".", $file_name); $va=count ($extend)-1; return $extend [$va];}

/ / method 4

Php code

Function getFileExt ($file_name)

{

While ($dot = strpos ($file_name, "."))

{

$file_name = substr ($file_name, $dot+1)

}

Return $file_name

}

? >

Function getFileExt ($file_name) {while ($dot = strpos ($file_name, ".")) {$file_name = substr ($file_name, $dot+1);} return $file_name;}? >

In addition:

PHP pathinfo () function

PHP Filesystem function

Definition and usage

The pathinfo () function returns information about the file path as an array.

Grammar.

Pathinfo (path,options)

Parameters.

Description

Path

Necessary. Specify the path to be checked.

Process_sections

Optional. Specifies the array elements to return. The default is all.

Possible values:

PATHINFO_DIRNAME-only dirname is returned

PATHINFO_BASENAME-only basename is returned

PATHINFO_EXTENSION-only extension is returned

Description

Pathinfo () returns an associative array containing information about path.

Includes the following array elements:

[dirname]

[basename]

[extension]

Tips and comments

Note: if all units are not required, the pathinfo () function returns a string.

Example Wuhan Renji Integrated traditional Chinese and Western Medicine Hospital http://www.whrjkf.com/

Example 1

Php code

/ / output:

/ / Array ([dirname] = > / testweb [basename] = > test.txt [extension] = > txt)

/ / output: / / Array ([dirname] = > / testweb [basename] = > test.txt [extension] = > txt)

After reading the above, have you mastered how to get the file extension in PHP? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Method code function array file extension example information element content more comment path problem output helpless to this end traditional Chinese and western medicine hospital unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL vpn Microsoft Shulou Tech Info