Home > Backend Development > PHP Tutorial > How to get the file extension in php_php tips

How to get the file extension in php_php tips

WBOY
Release: 2016-05-16 20:05:43
Original
1199 people have browsed it

There are many similar methods on the Internet, but they all have some imprecision problems. This article will not analyze them one by one. Here we only give the most correct method of using php to obtain the file extension (file suffix).

function get_extension($filename){ 
  return pathinfo($filename,PATHINFO_EXTENSION); 
} 
Copy after login

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:

<&#63;php 
print_r(pathinfo("/testweb/test.txt")); 
&#63;> 
Copy after login

The above will output the following Result:

Array(
 [dirname] => /testweb
 [basename] => test.txt
 [extension] => txt
)
Copy after login

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.

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