1
1
<?php
2
2
/*
3
- * jQuery File Upload Plugin PHP Class 5.11.2
3
+ * jQuery File Upload Plugin PHP Class 5.12
4
4
* https://github.com/blueimp/jQuery-File-Upload
5
5
*
6
6
* Copyright 2010, Sebastian Tschan
13
13
class UploadHandler
14
14
{
15
15
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
+ );
16
35
17
36
function __construct ($ options =null ) {
18
37
$ this ->options = array (
19
- 'script_url ' => $ this ->getFullUrl ().'/ ' ,
38
+ 'script_url ' => $ this ->get_full_url ().'/ ' ,
20
39
'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/files/ ' ,
21
- 'upload_url ' => $ this ->getFullUrl ().'/files/ ' ,
40
+ 'upload_url ' => $ this ->get_full_url ().'/files/ ' ,
22
41
'param_name ' => 'files ' ,
23
42
// Set the following option to 'POST', if your server does not support
24
43
// DELETE requests. This is a parameter sent to the client:
@@ -46,15 +65,15 @@ function __construct($options=null) {
46
65
/*
47
66
'large' => array(
48
67
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
49
- 'upload_url' => $this->getFullUrl ().'/files/',
68
+ 'upload_url' => $this->get_full_url ().'/files/',
50
69
'max_width' => 1920,
51
70
'max_height' => 1200,
52
71
'jpeg_quality' => 95
53
72
),
54
73
*/
55
74
'thumbnail ' => array (
56
75
'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/thumbnails/ ' ,
57
- 'upload_url ' => $ this ->getFullUrl ().'/thumbnails/ ' ,
76
+ 'upload_url ' => $ this ->get_full_url ().'/thumbnails/ ' ,
58
77
'max_width ' => 80 ,
59
78
'max_height ' => 80
60
79
)
@@ -65,7 +84,7 @@ function __construct($options=null) {
65
84
}
66
85
}
67
86
68
- protected function getFullUrl () {
87
+ protected function get_full_url () {
69
88
$ https = !empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' ;
70
89
return
71
90
($ https ? 'https:// ' : 'http:// ' ).
@@ -172,17 +191,22 @@ protected function create_scaled_image($file_name, $options) {
172
191
return $ success ;
173
192
}
174
193
194
+ protected function get_error_message ($ error ) {
195
+ return array_key_exists ($ error , $ this ->error_messages ) ?
196
+ $ this ->error_messages [$ error ] : $ error ;
197
+ }
198
+
175
199
protected function validate ($ uploaded_file , $ file , $ error , $ index ) {
176
200
if ($ error ) {
177
- $ file ->error = $ error ;
201
+ $ file ->error = $ this -> get_error_message ( $ error) ;
178
202
return false ;
179
203
}
180
204
if (!$ file ->name ) {
181
- $ file ->error = 'missingFileName ' ;
205
+ $ file ->error = $ this -> get_error_message ( 'missingFileName ' ) ;
182
206
return false ;
183
207
}
184
208
if (!preg_match ($ this ->options ['accept_file_types ' ], $ file ->name )) {
185
- $ file ->error = ' acceptFileTypes ' ;
209
+ $ file ->error = $ this -> get_error_message ( ' accept_file_types ' ) ;
186
210
return false ;
187
211
}
188
212
if ($ uploaded_file && is_uploaded_file ($ uploaded_file )) {
@@ -194,30 +218,36 @@ protected function validate($uploaded_file, $file, $error, $index) {
194
218
$ file_size > $ this ->options ['max_file_size ' ] ||
195
219
$ file ->size > $ this ->options ['max_file_size ' ])
196
220
) {
197
- $ file ->error = ' maxFileSize ' ;
221
+ $ file ->error = $ this -> get_error_message ( ' max_file_size ' ) ;
198
222
return false ;
199
223
}
200
224
if ($ this ->options ['min_file_size ' ] &&
201
225
$ file_size < $ this ->options ['min_file_size ' ]) {
202
- $ file ->error = ' minFileSize ' ;
226
+ $ file ->error = $ this -> get_error_message ( ' min_file_size ' ) ;
203
227
return false ;
204
228
}
205
229
if (is_int ($ this ->options ['max_number_of_files ' ]) && (
206
230
count ($ this ->get_file_objects ()) >= $ this ->options ['max_number_of_files ' ])
207
231
) {
208
- $ file ->error = ' maxNumberOfFiles ' ;
232
+ $ file ->error = $ this -> get_error_message ( ' max_number_of_files ' ) ;
209
233
return false ;
210
234
}
211
235
list ($ img_width , $ img_height ) = @getimagesize ($ uploaded_file );
212
236
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 ' );
216
247
return false ;
217
248
}
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 ' );
221
251
return false ;
222
252
}
223
253
}
0 commit comments