Skip to content

Commit

Permalink
Graceful degradation for mime type determination
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh Bragg committed Apr 5, 2013
1 parent 3d4ae07 commit e9a2fc6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions controller/site/thumbs.process.php
@@ -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);
Expand Down
5 changes: 2 additions & 3 deletions controller/site/userPicture.process.php
@@ -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.');
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions lib/getMimeType.php
@@ -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;
}
6 changes: 3 additions & 3 deletions model/classes/upload.class.php
@@ -1,5 +1,7 @@
<?php

require_once SYSTEM_PATH."/lib/getMimeType.php";

class Upload extends DbObject
{
protected $id;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e9a2fc6

Please sign in to comment.