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

How to get the file extension in php

墨辰丷
墨辰丷Original
2018-06-06 15:34:171760browse

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); 
}

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")); 
?>

The above will output the following Result:

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

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!

Statement:
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