[英]Determine excel file mime type
我需要確定什么是上傳文件的類型
上傳.xlsx
文件時,此代碼:
echo $_FILES['uploaded_file']['type']."<br>";
echo mime_content_type($_FILES['uploaded_file']['tmp_name']);
收益:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-excel
據我所知(從這里PHP xls,xlsx,ppt,pptx頭文件 ), application/vnd.ms-excel
不是.xlsx
,而是.xls
文件mime類型。
那么,為什么要為.xlsx
文件返回mime_content_type()
函數application/vnd.ms-excel
? 真相在哪里?
使用FileInfo而不是mime_content_type( 不推薦使用 )。
關於mime類型和擴展,
application/vnd.ms-excel xls xlb xlt
application/vnd.ms-excel.addin.macroEnabled.12 xlam
application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
application/vnd.ms-excel.template.macroEnabled.12 xltm
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
(可從linux webserver中的/etc/mime.types獲得)
mime_content_type()不是特別准確,並且已經棄用了Fileinfo()的mime_content_type ; 雖然我個人,我打開文件並明確測試文件中的某些數據元素,這些元素可能不包含在mime_magic簽名詳細信息中
這是一個正確識別Microsoft Office 2007文檔的包裝器。 使用,編輯和添加更多文件擴展/ mimetypes是微不足道的。
function get_mimetype($filepath) {
if(!preg_match('/\.[^\/\\\\]+$/',$filepath)) {
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);
}
switch(strtolower(preg_replace('/^.*\./','',$filepath))) {
// START MS Office 2007 Docs
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
case 'docm':
return 'application/vnd.ms-word.document.macroEnabled.12';
case 'dotx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
case 'dotm':
return 'application/vnd.ms-word.template.macroEnabled.12';
case 'xlsx':
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
case 'xlsm':
return 'application/vnd.ms-excel.sheet.macroEnabled.12';
case 'xltx':
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
case 'xltm':
return 'application/vnd.ms-excel.template.macroEnabled.12';
case 'xlsb':
return 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
case 'xlam':
return 'application/vnd.ms-excel.addin.macroEnabled.12';
case 'pptx':
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
case 'pptm':
return 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
case 'ppsx':
return 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
case 'ppsm':
return 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
case 'potx':
return 'application/vnd.openxmlformats-officedocument.presentationml.template';
case 'potm':
return 'application/vnd.ms-powerpoint.template.macroEnabled.12';
case 'ppam':
return 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
case 'sldx':
return 'application/vnd.openxmlformats-officedocument.presentationml.slide';
case 'sldm':
return 'application/vnd.ms-powerpoint.slide.macroEnabled.12';
case 'one':
return 'application/msonenote';
case 'onetoc2':
return 'application/msonenote';
case 'onetmp':
return 'application/msonenote';
case 'onepkg':
return 'application/msonenote';
case 'thmx':
return 'application/vnd.ms-officetheme';
//END MS Office 2007 Docs
}
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);
}
正如您在mime_content_type函數頁面上看到的警告,它現在已經過時 ,它被finfo函數取代。
$finfo = new finfo();
$fileinfo = $finfo->file($file, FILEINFO_MIME);
安裝finfo
擴展。
pecl install fileinfo
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.