1
1
<?php
2
2
/*
3
- * jQuery File Upload Plugin PHP Class 5.9.2
3
+ * jQuery File Upload Plugin PHP Class 5.10
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
-
16
+
17
17
function __construct ($ options =null ) {
18
18
$ this ->options = array (
19
19
'script_url ' => $ this ->getFullUrl ().'/ ' ,
@@ -68,7 +68,7 @@ protected function getFullUrl() {
68
68
$ _SERVER ['SERVER_PORT ' ] === 80 ? '' : ': ' .$ _SERVER ['SERVER_PORT ' ]))).
69
69
substr ($ _SERVER ['SCRIPT_NAME ' ],0 , strrpos ($ _SERVER ['SCRIPT_NAME ' ], '/ ' ));
70
70
}
71
-
71
+
72
72
protected function set_file_delete_url ($ file ) {
73
73
$ file ->delete_url = $ this ->options ['script_url ' ]
74
74
.'?file= ' .rawurlencode ($ file ->name );
@@ -77,7 +77,7 @@ protected function set_file_delete_url($file) {
77
77
$ file ->delete_url .= '&_method=DELETE ' ;
78
78
}
79
79
}
80
-
80
+
81
81
protected function get_file_object ($ file_name ) {
82
82
$ file_path = $ this ->options ['upload_dir ' ].$ file_name ;
83
83
if (is_file ($ file_path ) && $ file_name [0 ] !== '. ' ) {
@@ -96,7 +96,7 @@ protected function get_file_object($file_name) {
96
96
}
97
97
return null ;
98
98
}
99
-
99
+
100
100
protected function get_file_objects () {
101
101
return array_values (array_filter (array_map (
102
102
array ($ this , 'get_file_object ' ),
@@ -164,13 +164,19 @@ protected function create_scaled_image($file_name, $options) {
164
164
@imagedestroy ($ new_img );
165
165
return $ success ;
166
166
}
167
-
168
- protected function has_error ($ uploaded_file , $ file , $ error ) {
167
+
168
+ protected function validate ($ uploaded_file , $ file , $ error, $ index ) {
169
169
if ($ error ) {
170
- return $ error ;
170
+ $ file ->error = $ error ;
171
+ return false ;
172
+ }
173
+ if (!$ file ->name ) {
174
+ $ file ->error = 'missingFileName ' ;
175
+ return false ;
171
176
}
172
177
if (!preg_match ($ this ->options ['accept_file_types ' ], $ file ->name )) {
173
- return 'acceptFileTypes ' ;
178
+ $ file ->error = 'acceptFileTypes ' ;
179
+ return false ;
174
180
}
175
181
if ($ uploaded_file && is_uploaded_file ($ uploaded_file )) {
176
182
$ file_size = filesize ($ uploaded_file );
@@ -181,18 +187,21 @@ protected function has_error($uploaded_file, $file, $error) {
181
187
$ file_size > $ this ->options ['max_file_size ' ] ||
182
188
$ file ->size > $ this ->options ['max_file_size ' ])
183
189
) {
184
- return 'maxFileSize ' ;
190
+ $ file ->error = 'maxFileSize ' ;
191
+ return false ;
185
192
}
186
193
if ($ this ->options ['min_file_size ' ] &&
187
194
$ file_size < $ this ->options ['min_file_size ' ]) {
188
- return 'minFileSize ' ;
195
+ $ file ->error = 'minFileSize ' ;
196
+ return false ;
189
197
}
190
198
if (is_int ($ this ->options ['max_number_of_files ' ]) && (
191
199
count ($ this ->get_file_objects ()) >= $ this ->options ['max_number_of_files ' ])
192
200
) {
193
- return 'maxNumberOfFiles ' ;
201
+ $ file ->error = 'maxNumberOfFiles ' ;
202
+ return false ;
194
203
}
195
- return $ error ;
204
+ return true ;
196
205
}
197
206
198
207
protected function upcount_name_callback ($ matches ) {
@@ -209,8 +218,8 @@ protected function upcount_name($name) {
209
218
1
210
219
);
211
220
}
212
-
213
- protected function trim_file_name ($ name , $ type ) {
221
+
222
+ protected function trim_file_name ($ name , $ type, $ index ) {
214
223
// Remove path information and dots around the filename, to prevent uploading
215
224
// into different directories or replacing hidden system files.
216
225
// Also remove control characters and spaces (\x00..\x20) around the filename:
@@ -228,6 +237,10 @@ protected function trim_file_name($name, $type) {
228
237
return $ file_name ;
229
238
}
230
239
240
+ protected function handle_form_data ($ file , $ index ) {
241
+ // Handle form data, e.g. $_REQUEST['description'][$index]
242
+ }
243
+
231
244
protected function orient_image ($ file_path ) {
232
245
$ exif = @exif_read_data ($ file_path );
233
246
if ($ exif === false ) {
@@ -256,14 +269,14 @@ protected function orient_image($file_path) {
256
269
@imagedestroy ($ image );
257
270
return $ success ;
258
271
}
259
-
260
- protected function handle_file_upload ($ uploaded_file , $ name , $ size , $ type , $ error ) {
272
+
273
+ protected function handle_file_upload ($ uploaded_file , $ name , $ size , $ type , $ error, $ index ) {
261
274
$ file = new stdClass ();
262
- $ file ->name = $ this ->trim_file_name ($ name , $ type );
275
+ $ file ->name = $ this ->trim_file_name ($ name , $ type, $ index );
263
276
$ file ->size = intval ($ size );
264
277
$ file ->type = $ type ;
265
- $ error = $ this ->has_error ($ uploaded_file , $ file , $ error);
266
- if (! $ error && $ file -> name ) {
278
+ if ( $ this ->validate ($ uploaded_file , $ file , $ error, $ index )) {
279
+ $ this -> handle_form_data ( $ file , $ index );
267
280
$ file_path = $ this ->options ['upload_dir ' ].$ file ->name ;
268
281
$ append_file = !$ this ->options ['discard_aborted_uploads ' ] &&
269
282
is_file ($ file_path ) && $ file ->size > filesize ($ file_path );
@@ -310,12 +323,10 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
310
323
}
311
324
$ file ->size = $ file_size ;
312
325
$ this ->set_file_delete_url ($ file );
313
- } else {
314
- $ file ->error = $ error ;
315
326
}
316
327
return $ file ;
317
328
}
318
-
329
+
319
330
public function get () {
320
331
$ file_name = isset ($ _REQUEST ['file ' ]) ?
321
332
basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
@@ -327,7 +338,7 @@ public function get() {
327
338
header ('Content-type: application/json ' );
328
339
echo json_encode ($ info );
329
340
}
330
-
341
+
331
342
public function post () {
332
343
if (isset ($ _REQUEST ['_method ' ]) && $ _REQUEST ['_method ' ] === 'DELETE ' ) {
333
344
return $ this ->delete ();
@@ -347,7 +358,8 @@ public function post() {
347
358
$ _SERVER ['HTTP_X_FILE_SIZE ' ] : $ upload ['size ' ][$ index ],
348
359
isset ($ _SERVER ['HTTP_X_FILE_TYPE ' ]) ?
349
360
$ _SERVER ['HTTP_X_FILE_TYPE ' ] : $ upload ['type ' ][$ index ],
350
- $ upload ['error ' ][$ index ]
361
+ $ upload ['error ' ][$ index ],
362
+ $ index
351
363
);
352
364
}
353
365
} elseif ($ upload || isset ($ _SERVER ['HTTP_X_FILE_NAME ' ])) {
@@ -383,7 +395,7 @@ public function post() {
383
395
}
384
396
echo $ json ;
385
397
}
386
-
398
+
387
399
public function delete () {
388
400
$ file_name = isset ($ _REQUEST ['file ' ]) ?
389
401
basename (stripslashes ($ _REQUEST ['file ' ])) : null ;
0 commit comments