Home > Backend Development > PHP Tutorial > Two ways to get file extension in php

Two ways to get file extension in php

WBOY
Release: 2016-07-25 08:57:08
Original
917 people have browsed it
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


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