2013-04-23 112 views
13

我試圖讓Mime-Typeimage-types如下:調用未定義功能exif_imagetype()

if(!empty($_FILES['uploadfile']['name']) && $_FILES['uploadfile']['error'] == 0){  

    $file = $_FILES['uploadfile']['tmp_name']; 
    $file_type = image_type_to_mime_type(exif_imagetype($file)); 

    switch($file_type){ 

     // Codes Here 

    } 

} 

但它總是給人錯誤Call to undefined function exif_imagetype()。我在這裏做錯了什麼?

+0

@phpNoOb我試着作爲答案那裏,但仍然是相同的錯誤。 – 2013-04-23 17:42:57

回答

37

php.ini中啓用以下擴展並重新啓動服務器。

擴展=中php_mbstring.dll
延長= php_exif.dll

然後檢查phpinfo(),看它是否被設置爲開/關

+0

不要忘了在此操作後重新啓動你的apache服務器 – 2013-04-25 10:22:23

+0

--enable-exif#對於Linux – Zjmainstay 2014-04-02 05:20:50

+0

對於那些將是'extension = exif.so'的linux。我無法在php文檔中找到--enable-exif選項。 – BakaKuna 2015-10-10 15:51:50

3

添加到您的代碼,以便我們可以知道你使用哪個版本的PHP,因爲這個函數只支持(PHP版本4> = 4.3.0,PHP 5)。

<?php 
    phpinfo(); 
?> 

它可能沒有安裝,你可以添加這部分代碼,以確保它是:

<?php 
if (function_exists('exif_imagetype')) { 
    echo "This function is installed"; 
} else { 
    echo "It is not"; 
} 
?> 
+0

我在'PHP版本5.2.9'上。 – 2013-04-23 18:20:32

+0

@Kishor Subedi,檢查我的編輯,btw是你在Windows或Linux下? – 2013-04-25 09:59:35

+0

我是窗口用戶。 – 2013-04-26 05:30:20

3

我認爲這個問題是PHP配置和/或版本,例如,在我案例:

我們知道exif_imagetype()需要一個文件路徑或資源,並返回一個常數像IMAGETYPE_GIF 和image_type_to_mime_type()採用的是恆定值,並返回一個字符串'image/gif''image/jpeg'等 這沒有奏效(缺少函數exif_imagetype),所以我發現image_type_to_mime_type()也可以取整數1,2,3,17等作爲輸入, 這樣解決了使用getimagesize的問題,它返回一個整數值作爲MIME類型:

function get_image_type ($filename) { 
    $img = getimagesize($filename); 
    if (!empty($img[2])) 
     return image_type_to_mime_type($img[2]); 
return false; 
} 

echo get_image_type('my_ugly_file.bmp'); 
// returns image/x-ms-bmp 
echo get_image_type('path/pics/boobs.jpg'); 
// returns image/jpeg