This article mainly introduces in detail how PHP can obtain the file suffix name.
PHP Obtaining the file suffix name is a common operation for PHP learners, whether during the interview process or during self-study of PHP novices. Getting the file suffix name with PHP is a very common knowledge point that needs to be mastered.
Now we will give you a summary of PHP to obtain the file extension , which is the two methods of suffix name!
1. Through pathinfo
First of all, everyone should know that the pathinfo() function can return an associative array containing path information, then in the following code, we use pathinfo to obtain 1 The path information of the .txt file.
<?php var_dump(pathinfo('1.txt'));
The return information is as shown below:
#What we need to pay attention to in the picture is the extension element. The value of extension here is txt, which means File suffix name.
Then if we want to get the individual file suffix here, we can do the following:
1, PATHINFO_EXTENSION
<?php echo pathinfo('1.txt',PATHINFO_EXTENSION);
directly Use the PATHINFO_EXTENSION constant in pathinfo to obtain the suffix name
2 and array element
<?php $data = (pathinfo('1.txt')); echo $data['extension'];
. Just output the value of the array element directly here. The result of obtaining the file extension is the same as above.
2. Through substr
substr(), you can extract the specified number of characters starting from the start subscript in the string, which is to intercept one part of the string. Everyone should know about functions.
echo substr('1.txt',2);
Here we directly use substr to intercept and obtain the file suffix name. However, this method is best used only for simple file names. This substr method is not recommended for complex file names.
Generally involves the operation of obtaining the file name suffix. It is recommended to use the first method: pathinfo.
This article is about the specific method of obtaining the file suffix name in PHP. If you want to learn more about PHP, you can check out the PHP video tutorial on the PHP Chinese website.
The above is the detailed content of What are the ways to get the file extension in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!