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

How to get the file extension in php

墨辰丷
Release: 2023-03-30 09:00:02
Original
1815 people have browsed it

This article mainly introduces how to obtain the file suffix name in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

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

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
pathinfo() function takes the form of an array The form returns file path information.
Syntax
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")); 
?>
Copy after login

The above will output the following Result:

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Methods and examples of PHP implementation of paging class

Detailed explanation of methods and examples of PHP file upload class

php methods and examples for generating verification codes

The above is the detailed content of How to get the file extension in php. For more information, please follow other related articles on the PHP Chinese website!

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