2011-10-07 53 views
2

我有一個iPhone的Web服務。我以二進制形式從iPhone接收圖像。我想問的是我們能否確定以二進制形式發送的圖像的擴展名。PHP肥皂二進制圖像問題

我的代碼:

$data=base64_decode($data); 
$path='event_image/img_out.gif'; /// issue is here 
$fp=fopen($path,'w+'); 
if($fp){ fwrite($fp,$data); fclose($fp); } 
+0

可能重複的[PHP圖像類型檢測](http://stackoverflow.com/questions/6066951/php-image-type-detection) – zaf

回答

0

文件具有簽名http://www.garykessler.net/library/file_sigs.html 您可以從文件的第一行閱讀和檢查。

function output_jpeg($filename) 
{ 
if(($fp = fopen($filename, 「rb」)) === false) { 
return; 
} 
$line = fread($fp, 4); 
// check the ‘magic number’ of the file 
if($line === 「\377\330\377\340」) { 
fseek($fp, 0); 
fpassthru($fp); 
} 
fclose($fp); 
}