php 判斷圖片文件的真實(shí)類型

<?php
/**
 * 檢測(cè)文件的真實(shí)類型
 * @param  string $srcPath 文件路徑
 * @return string $realType 文件真實(shí)類型
*/

$imgurl = 'https://ae01.alicdn.com/kf/HTB1w5xdS4TpK1RjSZR0762EwXXa7.png';
echo file_type_detect($imgurl);
function file_type_detect($srcPath){
    $types = array(
        6677    => 'bmp',
        7173    => 'gif',
        7368    => 'mp3',
        13780   => 'png',
        255216  => 'jpg',
    );  

    $binary = curl_get($srcPath);
    $bytes = substr($binary, 0, 2); 
    $head = @unpack('C2char', $bytes);
    $typeCode = intval($head['char1'].$head['char2']);
    $realType = isset($types[$typeCode]) ? $types[$typeCode] : $typeCode;
    return $realType;
}
function curl_get($url){
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $content=curl_exec($ch);
    curl_close($ch);
    return($content);
}

轉(zhuǎn)載請(qǐng)注明出處 AE博客|墨淵 ? php 判斷圖片文件的真實(shí)類型

發(fā)表評(píng)論

路人甲

網(wǎng)友評(píng)論(1)

謝謝分享
手賺下載吧 5年前 (2019-12-06) 回復(fù)