国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php實現(xiàn)獲取文件mime類型的方法

Original 2017-02-08 11:40:34 336
abstract:本文實例講述了php獲取文件mime類型的方法。分享給大家供大家參考。具體如下:1.使用 mime_content_type 方法string mime_content_type ( string $filename )Returns the MIME content type for a file as determined by using i

本文實例講述了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.  

<?php
$mime_type = mime_content_type('1.jpg');
echo $mime_type; // image/jpeg
?>

但此方法在 php5.3 以上就被廢棄了,官方建議使用 fileinfo 方法代替。

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

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

<?php
$fi = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $fi->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

<?php
$image = exif_imagetype('1.jpg');
$mime_type = image_type_to_mime_type($image);
echo $mime_type; // image/jpeg
?>

Tips:如果使用文件名的后綴來判斷,因為文件后綴是可以修改的,所以使用文件后綴來判斷會不準確。

更多關(guān)于php實現(xiàn)獲取文件mime類型的方法請關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其他文章!

Release Notes

Popular Entries