如何在不依賴副檔名的情況下偵測檔案類型
除了檢查檔案的副檔名之外,確定檔案是mp3 還是圖像格式是很有價值的程式設計中的任務。這是一個不依賴擴充的全面解決方案:
PHP >= 5.3:
$mimetype = finfo_fopen(fopen($filename, 'r'), FILEINFO_MIME_TYPE);
PHP
$mimetype = mime_content_type($filename);
替代方案:
代理方法:
對於更通用的方法,請考慮將這些函數包裝到代理方法:
function getMimeType($filename)
{
$mimetype = false;
if (function_exists('finfo_fopen')) {
// open with FileInfo
} elseif (function_exists('getimagesize')) {
// open with GD
} elseif (function_exists('exif_imagetype')) {
// open with EXIF
} elseif (function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}
透過使用此代理程式方法,您可以根據系統上不同功能的可用性輕鬆確定檔案的 mimetype。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3