Home php教程 PHP源码 PHP中获取文件扩展名的N种方法小结_php技巧

PHP中获取文件扩展名的N种方法小结_php技巧

May 25, 2016 pm 05:12 PM
extension name

PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式

第1种方法:

代码如下:

function get_extension($file) 
{ 
substr(strrchr($file, '.'), 1); 
}
Copy after login


第2种方法:

代码如下:

function get_extension($file) 
{ 
return substr($file, strrpos($file, '.')+1); 
}
Copy after login


第3种方法:

代码如下:

function get_extension($file) 
{ 
return end(explode('.', $file)); 
}
Copy after login


第4种方法:

代码如下:

function get_extension($file) 
{ 
$info = pathinfo($file); 
return $info['extension']; 
}
Copy after login


第5种方法:

代码如下:

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


以上几种方式粗看了一下,好像都行,特别是1、2种方法,在我不知道pathinfo有第二个参数之前也一直在用。但是仔细考虑一下,前四种方法都有各种各样的毛病。要想完全正确获取文件的扩展名,必须要能处理以下三种特殊情况。
没有文件扩展名
路径中包含了字符.,如/home/test.d/test.txt
路径中包含了字符.,但文件没有扩展名。如/home/test.d/test
很明显:1、2不能处理第三种情况,3不能正确处理第一三种情况。4可以正确处理,但是在不存在扩展名时,会发出一个警告。只有第5种方法才是最正确的方法。顺便看一下pathinfo方法。官网上介绍如下:
$file_path = pathinfo('/www/htdocs/your_image.jpg');

echo "$file_path ['dirname']\n";
echo "$file_path ['basename']\n";
echo "$file_path ['extension']\n";
echo "$file_path ['filename']\n"; // only in PHP 5.2+
它会返回一个数组,包含最多四个元素,但是并不会一直有四个,比如在没有扩展名的情况下,就不会有extension元素存在,所以第4种方法才会发现警告。但是phpinfo还支持第二个参数。可以传递一个常量,指定返回某一部分的数据:
PATHINFO_DIRNAME - 目录
PATHINFO_BASENAME - 文件名(含扩展名)
PATHINFO_EXTENSION - 扩展名
PATHINFO_FILENAME - 文件名(不含扩展名,PHP>5.2)
这四个常量的值分别是1、2、4、8,刚开始我还以为可以通过或运算指定多个:
pathinfo($file, PATHINFO_EXTENSION | PATHINFO_FILENAME);
后来发现这样不行,这只会返回几个进行或运算常量中最小的那个。也就是四个标志位中最小位为1的常量。
以下是一些补充方法
一,php explode函数

代码如下:

$pic = 'abc.php'; 
$pics = explode('.' , $pic); 
echo $num = count($pics); 
echo &#39;<br>&#39;.$pics[$num-1];
Copy after login



这样就可以输出
.php了。

下面利用foreach

代码如下:

foreach ($pics as $value) //2 
{ 
$a = $value; 
} 
echo $a.&#39;<br>&#39;;
Copy after login



来有一个比较好的函数end我推荐使用此函数快捷 end函数用法

代码如下:

echo end($pics); 
echo &#39;<br>&#39;;
Copy after login



其它的访方法可以在文件上传时判断,不过那需要文件上传不上传我们不能使用$_FILES来操作。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

4 Quick Ways to Open Webp Images on Windows 11 4 Quick Ways to Open Webp Images on Windows 11 Sep 30, 2023 pm 02:13 PM

WebP or WebPicture is a modern image format developed by Google and has been widely used by the industry recently. These images are smaller than JPG, JPEG or PNG files, which increases page load speed and makes them valuable from an SEO perspective. But how to open WebP files in Windows 11? Given that it's a relatively new image format, first announced in March 2010, compatibility is bound to be an issue. Some photo editing software still don't support file extensions. But things change quickly! Does Windows 11 support WebP? Although WebP image files are not compatible with previous Windows 11, M

How to fix file names or extensions that are too long in Windows 11? How to fix file names or extensions that are too long in Windows 11? Apr 22, 2023 pm 04:37 PM

Have you ever faced any issues while transferring files that prevented you from doing so? Well, many Windows users have recently reported that they faced issues while copying and pasting files into a folder, where an error was thrown saying “The file name of the destination folder is too long”. Additionally, some other Windows users expressed frustration when opening any file and said "the file name or extension is too long" and they were unable to open the file. This disappoints users by not allowing them to transfer files to any other folder. While analyzing the issue, we have come up with a series of solutions that may help alleviate the issue and allow users to transfer files easily. If you are in a similar situation, please see this post for more information. Source: https

How to open files without extension in Windows 11, 10 How to open files without extension in Windows 11, 10 Apr 14, 2023 pm 05:52 PM

Having a file doesn't mean much unless you know its extension. Some files have no extension. Most of the time, the extension is probably just hidden and you can easily find it from Windows File Explorer itself using the methods listed in Section 1. Even if the file doesn't actually have an extension, it can be easily found by following the methods listed in Section 2. Finally, once you know the file extension, you can follow the steps in Section 3 to find an appropriate application to open your file. So, this article is a complete solution to all your file extension problems. Hope you enjoy reading! Section 1: How to Determine if a File Has an Extension The first step in opening a file without an extension is to make sure that the file actually does not have an extension. this

How to deal with inconsistent file formats and extensions How to deal with inconsistent file formats and extensions Feb 19, 2024 pm 04:47 PM

How to solve the mismatch between file format and extension. During computer use, we often encounter mismatch between file format and extension. When we try to open a file, the system may prompt "The file cannot be opened" or "The file format is not supported." This situation can be confusing and frustrating, but it's actually not that difficult to fix and it only requires a few simple steps. First, we need to understand what the file format and extension mean. File format refers to the storage method and encoding method of the file. For example, a text file is

How to rename the extension of all files within a folder, including subfolders How to rename the extension of all files within a folder, including subfolders Apr 14, 2023 pm 12:22 PM

Suppose you need to rename the extension of a file from one extension to another, say jpg to png. It's easy, of course! But what if you have multiple files whose extensions need to be changed? Or worse, what if these multiple files are also located in multiple folders and subfolders, within a single folder? Well, for a normal person, this can be a nightmare. But for a geek, absolutely not. The question now is, are you a geek? Well, with the help of Geek Page, you definitely are! In this article, we explain how to easily rename the extension of all files within a folder, including subfolders of your choice, from one extension to another through a batch script method. Notice:

What is the document suffix? What is the document suffix? Mar 01, 2023 pm 03:06 PM

The document suffix, also called the file extension, is a mechanism used by the operating system to mark file types; the main function of the document suffix is ​​to let the system decide what software should be used to run when the user opens the specified file, so it can be used Used to protect certain files, save disk space, clear icons on the settings panel, and set associations.

Use the path/filepath.Ext function to get the extension part of the file path Use the path/filepath.Ext function to get the extension part of the file path Jul 25, 2023 pm 08:42 PM

Use the path/filepath.Ext function to obtain the extension part of the file path. During the programming process, we often encounter the need to obtain the extension of the file. Go language provides a very convenient function path/filepath.Ext to implement this function. This article explains how to use this function to get the extension part of a file path. First, let's look at a simple example: packagemainimport("fmt&q

What file types can be identified based on What file types can be identified based on Jul 19, 2022 pm 04:45 PM

The type of file can be identified based on the "file extension". File extension is a mechanism, or symbol, used by the operating system to mark file types; the extension is almost an essential part of every file. If a file does not have an extension, the operating system cannot identify the file and cannot distinguish it. What type does it belong to, so it is impossible to determine which software to use to process this file.

See all articles