Permalink
Browse files

Graceful degradation for mime type determination

  • Loading branch information...
1 parent 3d4ae07 commit e9a2fc66d635a976962471b95e6252f4361dcecf Hugh Bragg committed Apr 5, 2013
@@ -1,12 +1,12 @@
<?php
+require_once SYSTEM_PATH."/lib/getMimeType.php";
+
// temp variable for uploaded file (and path)
$uploadedFile = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// get MIME type
-$finfo = finfo_open(FILEINFO_MIME_TYPE);
-$mime = finfo_file($finfo, $uploadedFile);
-finfo_close($finfo);
+$mime = getMimeType($uploadedFile);
// get extension
$ext = pathinfo($uploadedFile, PATHINFO_EXTENSION);
@@ -1,5 +1,6 @@
<?php
require_once("../../global.php");
+require_once SYSTEM_PATH."/lib/getMimeType.php";
if(!Session::isLoggedIn()) {
$json = array('error' => 'You must be logged in.');
@@ -168,9 +169,7 @@
$uploadedFile = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// get MIME type
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $mime = finfo_file($finfo, $uploadedFile);
- finfo_close($finfo);
+ $mime = getMimeType($uploadedFile);
// get extension
$ext = pathinfo($uploadedFile, PATHINFO_EXTENSION);
View
@@ -0,0 +1,24 @@
+<?php
+// Hugh Bragg
+function getMimeType($filename)
+{
+ $mimetype = false;
+ if(function_exists('finfo_open')) {
+ // open with FileInfo
+ $finfo = finfo_open(FILEINFO_MIME_TYPE);
+ $mimetype = finfo_file($finfo, $filename);
+ finfo_close($finfo);
+ } elseif(function_exists('exif_imagetype')) {
+ // open with EXIF
+ $extifType = exif_imagetype($filename);
+ $mimetype = image_type_to_mime_type($extifType);
+ } elseif(function_exists('getimagesize')) {
+ // open with GD
+ $info = getimagesize($filename);
+ $mimetype = $info['mime'];
+ } elseif(function_exists('mime_content_type')) {
+ // PHP < 5.3
+ $mimetype = mime_content_type($filename);
+ }
+ return $mimetype;
+}
@@ -1,5 +1,7 @@
<?php
+require_once SYSTEM_PATH."/lib/getMimeType.php";
+
class Upload extends DbObject
{
protected $id;
@@ -190,9 +192,7 @@ public static function saveToDatabase($originalName=null, $storedName=null, $ite
// get file size
$size = filesize($absPath);
// get mime type
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $mime = finfo_file($finfo, $absPath);
- finfo_close($finfo);
+ $mime = getMimeType($absPath);
// get height and width (if image)
$imgSize = getimagesize($absPath);
if($imgSize) {

0 comments on commit e9a2fc6

Please sign in to comment.