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?

王林
Release: 2023-02-23 14:30:01
Original
3887 people have browsed it

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.$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

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

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

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template