This article introduces a piece of PHP code used to obtain the file extension. To obtain the extension in PHP, it is more convenient to use the pathinfo() method. Friends in need can refer to it.
In php, you can easily get the file extension from the end of the file. You can do this using the PHP function pathinfo. When using it, pay attention to the period before the file extension. The extension obtained using pathinfo() does not include the period. The following introduces two methods of obtaining file extensions, as follows. Method 1, <?php /*** example usage ***/ $filename = 'filename.blah.txt'; /*** get the path info ***/ $info = pathinfo($filename); /*** show the extension ***/ echo $info['extenstion']; ?> Copy after login Method 2 is basically the same as method 1, but it uses string operations to get the extension and uses the . sign as the separator. Code: <?php /*** example usage ***/ $filename = 'filename.blah.txt'; echo getFileExtension($filename); /** * * @Get File extension from file name * * @param string $filename the name of the file * * @return string * **/ function getFileExtension($filename){ return substr($filename, strrpos($filename, '.')); } ?> Copy after login |