Home Backend Development PHP Problem What are the functions in php to get the file suffix name?

What are the functions in php to get the file suffix name?

Sep 02, 2019 am 10:33 AM
php function File extension

What are the functions in php to get the file suffix name?

php file upload and multi-file upload are important and must be mastered. There are many methods for file upload. Here we mainly introduce several of them for your reference. For everyone’s reference.

1. String search and segmentation method

1

2

1.$file = 'x.y.z.png';

echo substr(strrchr($file, '.'), 1);

Copy after login

Analysis: strrchr($file, '.')
strrchr() function searches for strings The position of the last occurrence in another string and returns all characters from that position to the end of the string

1

2

2.$file = 'x.y.z.png';

echo substr($file, strrpos($file, '.')+1);

Copy after login

Parsing: strrpos($file, '.')
Find " ." At the last position in the string, return the position substr() intercepts from this position

2. Array splitting method

1

2

3

4

5

6

7

8

3.$file = 'x.y.z.png';

$arr=explode('.', $file);

echo $arr[count($arr)-1];

4.$file = 'x.y.z.png';

$arr=explode('.', $file);

echo end($arr); 

5.$file = 'x.y.z.png';

echo strrev(explode('.', strrev($file))[0]);

Copy after login

Analysis :end() returns the last element of the array, and the strrev(string) function reverses the string.

3. Path function method

1

2

3

4

5

6

7

8

6.$file = 'x.y.z.png';

echo pathinfo($file)['extension'];

//参数如下

[dirname]

[basename]

[extension]

7.$file = 'x.y.z.png';

echo pathinfo($file, PATHINFO_EXTENSION);

Copy after login

Analysis: The pathinfo() function returns the file path information in the form of an array.

The above introduces a total of 7 methods to obtain file extension names, for your reference only. If you want to know more related issues, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of What are the functions in php to get the file suffix name?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize the lazy loading effect of images through php functions? How to optimize the lazy loading effect of images through php functions? Oct 05, 2023 pm 12:13 PM

How to optimize the lazy loading effect of images through php functions?

What file is the suffix bin? What file is the suffix bin? Feb 25, 2024 pm 08:45 PM

What file is the suffix bin?

PHP Deprecated: Function ereg_replace() is deprecated - Solution PHP Deprecated: Function ereg_replace() is deprecated - Solution Aug 18, 2023 am 10:48 AM

PHP Deprecated: Function ereg_replace() is deprecated - Solution

How to reduce memory usage through php functions? How to reduce memory usage through php functions? Oct 05, 2023 pm 01:45 PM

How to reduce memory usage through php functions?

Introduction to PHP functions: strtr() function Introduction to PHP functions: strtr() function Nov 03, 2023 pm 12:15 PM

Introduction to PHP functions: strtr() function

Similarities and differences between PHP functions and Flutter functions Similarities and differences between PHP functions and Flutter functions Apr 24, 2024 pm 01:12 PM

Similarities and differences between PHP functions and Flutter functions

Win11 file suffix display operation guide Win11 file suffix display operation guide Mar 09, 2024 am 09:18 AM

Win11 file suffix display operation guide

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

Comparing PHP functions to functions in other languages

See all articles