Home  >  Article  >  Daily Programming  >  What are the ways to get the file extension in PHP? (Pictures + Videos)

What are the ways to get the file extension in PHP? (Pictures + Videos)

藏色散人
藏色散人Original
2018-09-30 09:24:266488browse

This article mainly introduces in detail how PHP can obtain the file suffix name.

PHP Obtaining the file suffix name is a common operation for PHP learners, whether during the interview process or during self-study of PHP novices. Getting the file suffix name with PHP is a very common knowledge point that needs to be mastered.

Now we will give you a summary of PHP to obtain the file extension , which is the two methods of suffix name!

1. Through pathinfo

First of all, everyone should know that the pathinfo() function can return an associative array containing path information, then in the following code, we use pathinfo to obtain 1 The path information of the .txt file.

<?php
var_dump(pathinfo(&#39;1.txt&#39;));

The return information is as shown below:

What are the ways to get the file extension in PHP? (Pictures + Videos)

#What we need to pay attention to in the picture is the extension element. The value of extension here is txt, which means File suffix name.

Then if we want to get the individual file suffix here, we can do the following:

1, PATHINFO_EXTENSION

<?php
echo pathinfo(&#39;1.txt&#39;,PATHINFO_EXTENSION);

directly Use the PATHINFO_EXTENSION constant in pathinfo to obtain the suffix name

2 and array element

<?php
$data = (pathinfo(&#39;1.txt&#39;));
echo $data[&#39;extension&#39;];

. Just output the value of the array element directly here. The result of obtaining the file extension is the same as above.

2. Through substr

substr(), you can extract the specified number of characters starting from the start subscript in the string, which is to intercept one part of the string. Everyone should know about functions.

echo substr(&#39;1.txt&#39;,2);

Here we directly use substr to intercept and obtain the file suffix name. However, this method is best used only for simple file names. This substr method is not recommended for complex file names.

Generally involves the operation of obtaining the file name suffix. It is recommended to use the first method: pathinfo.

This article is about the specific method of obtaining the file suffix name in PHP. If you want to learn more about PHP, you can check out the PHP video tutorial on the PHP Chinese website.

The above is the detailed content of What are the ways to get the file extension in PHP? (Pictures + Videos). 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