2012-06-06 133 views
0

我的系統上保存了一些沒有文件擴展名的圖像,它們以「FILENAME」格式顯示。 (注意期間)確定缺少擴展名的圖像的文件擴展名

我想使用getimagesize();但示數出來告訴我,「文件名不能爲空」

下面是代碼的樣本我使用

$contents = file_get_contents($localFile); 

    $imageData = getimagesize($contents); 
    // $imageData[2] will contain the value of one of the constants 
    $mimeType = image_type_to_mime_type($imageData[2]); 
    $extension = image_type_to_extension($imageData[2]); 
+0

你的圖像應該有擴展名(例如GIF,JPG,PNG等)。並非所有瀏覽器都可能將MIME類型返回到PHP腳本,因此您最好創建一個允許的擴展數組(例如$ allowedExts = array('jpg','gif','png')),然後展開圖像的文件名(例如$ tmp = explode(「。」,$ filename)),並使用'in_array($ tmp,$ allowedExts)'檢查上傳的文件是否爲圖像。如果是這樣,那麼做任何你想要的。 – Bob

回答

1

看文檔:http://php.net/getimagesize
getimagesize()期望將文件名作爲第一個參數,而不是映像文件的內容。

嘗試:

$imageData = getimagesize($localFile); 
// $imageData[2] will contain the value of one of the constants 
$mimeType = image_type_to_mime_type($imageData[2]); 
$extension = image_type_to_extension($imageData[2]);