There are many similar methods on the Internet, but they all have some imprecise problems. This article will not analyze them one by one. Here Only the most correct method of using php to obtain the file extension (file suffix) is given.
function get_extension($filename){ return pathinfo($filename,PATHINFO_EXTENSION); }
The PHP built-in function pathinfo is used in the function. Let’s analyze the meaning and usage of this function:
Definition and usage
The pathinfo() function returns file path information in the form of an array.
Grammar
pathinfo(path,options)
Description: pathinfo() returns an associative array containing path information. Array elements include the following values:
[dirname]
[basename]
[extension]
For example:
<?php print_r(pathinfo("/testweb/test.txt")); ?>
The above will output the following Result:
Array( [dirname] => /testweb [basename] => test.txt [extension] => txt )
The above is the method to correctly use php to obtain the file extension (file suffix name). I hope you like it and apply what you have learned.