Skip to content

Commit 0f75b1b

Browse files
committed
Tighten up file upload error reporting
1 parent 3aed7d3 commit 0f75b1b

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

system/application/controllers/book.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,15 @@ private function upload() {
414414

415415
$this->data['view'] = __FUNCTION__;
416416

417-
if ($action == 'add' || $action == 'replace') {
418-
$return = array('error'=>'');
417+
if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && 'post'==strtolower($_SERVER['REQUEST_METHOD'])) {
418+
419+
echo json_encode( array('error'=>'The file is larger than the server\'s max upload size') );
420+
exit;
421+
422+
} elseif ($action == 'add' || $action == 'replace') {
423+
424+
$return = array();
425+
419426
try {
420427
$slug = confirm_slash($this->data['book']->slug);
421428
$url = $this->file_upload->uploadMedia($slug, $chmod_mode, $this->versions);
@@ -427,16 +434,20 @@ private function upload() {
427434
exit;
428435
}
429436

430-
$this->load->library('Image_Metadata', 'image_metadata');
431-
$return = array();
432-
$return[$url] = $this->image_metadata->get($path, Image_Metadata::FORMAT_NS);
437+
try {
438+
$this->load->library('Image_Metadata', 'image_metadata');
439+
$return[$url] = $this->image_metadata->get($path, Image_Metadata::FORMAT_NS);
440+
} catch (Exception $e) {
441+
// Don't throw exception since this isn't critical
442+
}
443+
433444
if (false!==$thumbUrl) {
434445
$return[$url]['scalar:thumbnail'] = confirm_slash(base_url()).$slug.$thumbUrl;
435446
}
436447
echo json_encode($return);
437448
exit;
438449

439-
}
450+
} // if
440451

441452
// List of media pages
442453
$this->data['book_media'] = $this->pages->get_all($this->data['book']->book_id, 'media', null, false);

system/application/libraries/File_Upload.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ class File_Upload {
44

55
const THUMB_WIDTH = 120;
66

7-
public function __construct() {
8-
9-
}
10-
private function upload($tempFile,$targetFile,$chmodMode) {
11-
if (!move_uploaded_file($tempFile,$targetFile)) throw new Exception('Problem moving temp file. The file could be too large.');
12-
@chmod($targetFile, $chmodMode);
13-
}
7+
public function __construct() {}
148

159
public function uploadMedia($slug,$chmodMode,$versions=null) {
1610
if (empty($_FILES)) throw new Exception('Could not find uploaded file');
@@ -76,9 +70,44 @@ public function uploadPublisherThumb($slug,$chmodMode) {
7670
return 'media/'.$targetName;
7771
}
7872

73+
private function upload($tempFile,$targetFile,$chmodMode) {
74+
if (!move_uploaded_file($tempFile,$targetFile)) throw new Exception('Problem moving temp file. The file is likely larger than the system\'s max upload size ('.$this->getMaximumFileUploadSize().').');
75+
@chmod($targetFile, $chmodMode);
76+
}
77+
7978
private function resize($targetFile,$width) {
8079
require confirm_slash(APPPATH).'libraries/wideimage/WideImage.php';
8180
WideImage::load($targetFile)->resize($width)->saveToFile($targetFile);
8281
}
82+
83+
private function convertPHPSizeToBytes($sSize) { // http://stackoverflow.com/questions/13076480/php-get-actual-maximum-upload-size
84+
if ( is_numeric( $sSize) ) {
85+
return $sSize;
86+
}
87+
$sSuffix = substr($sSize, -1);
88+
$iValue = substr($sSize, 0, -1);
89+
switch(strtoupper($sSuffix)){
90+
case 'P':
91+
$iValue *= 1024;
92+
case 'T':
93+
$iValue *= 1024;
94+
case 'G':
95+
$iValue *= 1024;
96+
case 'M':
97+
$iValue *= 1024;
98+
case 'K':
99+
$iValue *= 1024;
100+
break;
101+
}
102+
return $iValue;
103+
}
104+
105+
private function getMaximumFileUploadSize() { // http://stackoverflow.com/questions/13076480/php-get-actual-maximum-upload-size
106+
$size = min($this->convertPHPSizeToBytes(ini_get('post_max_size')), $this->convertPHPSizeToBytes(ini_get('upload_max_filesize')));
107+
$base = log($size) / log(1024);
108+
$suffix = array("", "k", "M", "G", "T")[floor($base)];
109+
return pow(1024, $base - floor($base)) . $suffix;
110+
}
111+
83112
}
84113
?>

system/application/libraries/Image_Metadata.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Image_Metadata {
88
private $iptc_ns = 'iptc';
99
private $iptc_uri = '';
1010
private $iptc = array();
11+
private $exif_ns = 'exif';
12+
private $exif_uri = '';
13+
private $exif = array();
1114

1215
public function __construct() {
1316

@@ -25,7 +28,7 @@ public function get($path='', $format=Image_Metadata::FORMAT_NS) {
2528
$return = array();
2629

2730
$iptc_arr = (!empty($this->iptc_ns)) ? $this->get_iptc($path) : array();
28-
//$exif_arr = $this->get_exif($path);
31+
$exif_arr = (!empty($this->exif_ns)) ? $this->get_exif($path) : array();
2932
foreach ($iptc_arr as $field => $value) {
3033
switch ($format) {
3134
case Image_Metadata::FORMAT_NS:
@@ -43,7 +46,9 @@ public function get($path='', $format=Image_Metadata::FORMAT_NS) {
4346

4447
private function get_exif($path='') {
4548

46-
$exif = exif_read_data($path);
49+
// $exif = exif_read_data($path);
50+
// TODO
51+
return array();
4752

4853
}
4954

0 commit comments

Comments
 (0)