Skip to content

Commit 8bbfb7a

Browse files
committed
Return meaningful error messages. Fixes blueimp#1720.
1 parent ed7ccce commit 8bbfb7a

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

server/php/upload.class.php

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 5.11.2
3+
* jQuery File Upload Plugin PHP Class 5.12
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -13,12 +13,31 @@
1313
class UploadHandler
1414
{
1515
protected $options;
16+
// PHP File Upload error message codes:
17+
// http://php.net/manual/en/features.file-upload.errors.php
18+
protected $error_messages = array(
19+
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
20+
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
21+
3 => 'The uploaded file was only partially uploaded',
22+
4 => 'No file was uploaded',
23+
6 => 'Missing a temporary folder',
24+
7 => 'Failed to write file to disk',
25+
8 => 'A PHP extension stopped the file upload',
26+
'max_file_size' => 'File is too big',
27+
'min_file_size' => 'File is too small',
28+
'accept_file_types' => 'Filetype not allowed',
29+
'max_number_of_files' => 'Maximum number of files exceeded',
30+
'max_width' => 'Image exceeds maximum width',
31+
'min_width' => 'Image requires a minimum width',
32+
'max_height' => 'Image exceeds maximum height',
33+
'min_height' => 'Image requires a minimum height'
34+
);
1635

1736
function __construct($options=null) {
1837
$this->options = array(
19-
'script_url' => $this->getFullUrl().'/',
38+
'script_url' => $this->get_full_url().'/',
2039
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
21-
'upload_url' => $this->getFullUrl().'/files/',
40+
'upload_url' => $this->get_full_url().'/files/',
2241
'param_name' => 'files',
2342
// Set the following option to 'POST', if your server does not support
2443
// DELETE requests. This is a parameter sent to the client:
@@ -46,15 +65,15 @@ function __construct($options=null) {
4665
/*
4766
'large' => array(
4867
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
49-
'upload_url' => $this->getFullUrl().'/files/',
68+
'upload_url' => $this->get_full_url().'/files/',
5069
'max_width' => 1920,
5170
'max_height' => 1200,
5271
'jpeg_quality' => 95
5372
),
5473
*/
5574
'thumbnail' => array(
5675
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/thumbnails/',
57-
'upload_url' => $this->getFullUrl().'/thumbnails/',
76+
'upload_url' => $this->get_full_url().'/thumbnails/',
5877
'max_width' => 80,
5978
'max_height' => 80
6079
)
@@ -65,7 +84,7 @@ function __construct($options=null) {
6584
}
6685
}
6786

68-
protected function getFullUrl() {
87+
protected function get_full_url() {
6988
$https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
7089
return
7190
($https ? 'https://' : 'http://').
@@ -172,17 +191,22 @@ protected function create_scaled_image($file_name, $options) {
172191
return $success;
173192
}
174193

194+
protected function get_error_message($error) {
195+
return array_key_exists($error, $this->error_messages) ?
196+
$this->error_messages[$error] : $error;
197+
}
198+
175199
protected function validate($uploaded_file, $file, $error, $index) {
176200
if ($error) {
177-
$file->error = $error;
201+
$file->error = $this->get_error_message($error);
178202
return false;
179203
}
180204
if (!$file->name) {
181-
$file->error = 'missingFileName';
205+
$file->error = $this->get_error_message('missingFileName');
182206
return false;
183207
}
184208
if (!preg_match($this->options['accept_file_types'], $file->name)) {
185-
$file->error = 'acceptFileTypes';
209+
$file->error = $this->get_error_message('accept_file_types');
186210
return false;
187211
}
188212
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
@@ -194,30 +218,36 @@ protected function validate($uploaded_file, $file, $error, $index) {
194218
$file_size > $this->options['max_file_size'] ||
195219
$file->size > $this->options['max_file_size'])
196220
) {
197-
$file->error = 'maxFileSize';
221+
$file->error = $this->get_error_message('max_file_size');
198222
return false;
199223
}
200224
if ($this->options['min_file_size'] &&
201225
$file_size < $this->options['min_file_size']) {
202-
$file->error = 'minFileSize';
226+
$file->error = $this->get_error_message('min_file_size');
203227
return false;
204228
}
205229
if (is_int($this->options['max_number_of_files']) && (
206230
count($this->get_file_objects()) >= $this->options['max_number_of_files'])
207231
) {
208-
$file->error = 'maxNumberOfFiles';
232+
$file->error = $this->get_error_message('max_number_of_files');
209233
return false;
210234
}
211235
list($img_width, $img_height) = @getimagesize($uploaded_file);
212236
if (is_int($img_width)) {
213-
if ($this->options['max_width'] && $img_width > $this->options['max_width'] ||
214-
$this->options['max_height'] && $img_height > $this->options['max_height']) {
215-
$file->error = 'maxResolution';
237+
if ($this->options['max_width'] && $img_width > $this->options['max_width']) {
238+
$file->error = $this->get_error_message('max_width');
239+
return false;
240+
}
241+
if ($this->options['max_height'] && $img_height > $this->options['max_height']) {
242+
$file->error = $this->get_error_message('max_height');
243+
return false;
244+
}
245+
if ($this->options['min_width'] && $img_width < $this->options['min_width']) {
246+
$file->error = $this->get_error_message('min_width');
216247
return false;
217248
}
218-
if ($this->options['min_width'] && $img_width < $this->options['min_width'] ||
219-
$this->options['min_height'] && $img_height < $this->options['min_height']) {
220-
$file->error = 'minResolution';
249+
if ($this->options['min_height'] && $img_height < $this->options['min_height']) {
250+
$file->error = $this->get_error_message('min_height');
221251
return false;
222252
}
223253
}

0 commit comments

Comments
 (0)