search
Homephp教程php手册php实现获取文件mime类型的方法

这篇文章主要介绍了php实现获取文件mime类型的方法,实例分析了php实现获取文件mime类型的各种常见技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php获取文件mime类型的方法。分享给大家供大家参考。具体如下:

1.使用 mime_content_type 方法

string mime_content_type ( string $filename )

Returns the MIME content type for a file as determined by using information from the magic.mime file.  

但此方法在 php5.3 以上就被废弃了,,官方建议使用 fileinfo 方法代替。

2.使用 Fileinfo 方法 (官方推荐)

使用fileinfo需要安装php_fileinfo扩展。
如已安装可以在extension_dir目录下找到php_fileinfo.dll(windows),fileinfo.so(linux)
打开php.ini,把extension=php_fileinfo.dll前的";"去掉,然后重启apache。

file('1.jpg'); echo $mime_type; // image/jpeg ?>

3.使用 image_type_to_mime_type 方法(只能处理图象类型)

使用exif_imagetype方法需要安装php_exif扩展,并需要安装php_mbstring扩展
如已安装可以在extension_dir目录下找到php_exif.dll(windows),exif.so(linux)
打开php.ini,把 extension=php_mbstring.dll, extension=php_exif.dll 前的","去掉,然后重启apache

Tips:如果使用文件名的后缀来判断,因为文件后缀是可以修改的,所以使用文件后缀来判断会不准确。

希望本文所述对大家的php程序设计有所帮助。

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

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 Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.