/
fileUpload.lib.php
executable file
·2257 lines (2033 loc) · 75.5 KB
/
fileUpload.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* For licensing terms, see /license.txt */
/**
* FILE UPLOAD LIBRARY.
*
* This is the file upload library for Chamilo.
* Include/require it in your code to use its functionality.
*
* @package chamilo.library
*
* @todo test and reorganise
*/
use enshrined\svgSanitize\Sanitizer;
/**
* Changes the file name extension from .php to .phps
* Useful for securing a site.
*
* @author Hugues Peeters <peeters@ipm.ucl.ac.be>
*
* @param string $file_name Name of a file
*
* @return string the filename phps'ized
*/
function php2phps($file_name)
{
return preg_replace('/\.(phar.?|php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
}
/**
* Renames .htaccess & .HTACCESS to htaccess.txt.
*
* @param string $filename
*
* @return string
*/
function htaccess2txt($filename)
{
return str_replace(['.htaccess', '.HTACCESS'], ['htaccess.txt', 'htaccess.txt'], $filename);
}
/**
* This function executes our safety precautions
* more functions can be added.
*
* @param string $filename
*
* @return string
*
* @see php2phps()
* @see htaccess2txt()
*/
function disable_dangerous_file($filename)
{
return htaccess2txt(php2phps($filename));
}
/**
* Returns the name without extension, used for the title.
*
* @param string $name
*
* @return name without the extension
*/
function get_document_title($name)
{
// If they upload .htaccess...
$name = disable_dangerous_file($name);
$ext = substr(strrchr($name, '.'), 0);
if (empty($ext)) {
return substr($name, 0, strlen($name));
}
return substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
}
/**
* This function checks if the upload succeeded.
*
* @param array $uploaded_file ($_FILES)
*
* @return true if upload succeeded
*/
function process_uploaded_file($uploaded_file, $show_output = true)
{
// Checking the error code sent with the file upload.
if (isset($uploaded_file['error'])) {
switch ($uploaded_file['error']) {
case 1:
// The uploaded file exceeds the upload_max_filesize directive in php.ini.
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize'),
'error'
)
);
}
return false;
case 2:
// The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
// Not used at the moment, but could be handy if we want to limit the size of an upload
// (e.g. image upload in html editor).
$max_file_size = (int) $_POST['MAX_FILE_SIZE'];
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplExceedMaxPostSize').format_file_size($max_file_size),
'error'
)
);
}
return false;
case 3:
// The uploaded file was only partially uploaded.
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain'),
'error'
)
);
}
return false;
case 4:
// No file was uploaded.
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplNoFileUploaded').' '.get_lang('UplSelectFileFirst'),
'error'
)
);
}
return false;
}
}
if (!file_exists($uploaded_file['tmp_name'])) {
// No file was uploaded.
if ($show_output) {
Display::addFlash(Display::return_message(get_lang('UplUploadFailed'), 'error'));
}
return false;
}
if (file_exists($uploaded_file['tmp_name'])) {
$filesize = filesize($uploaded_file['tmp_name']);
if (empty($filesize)) {
// No file was uploaded.
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadFailedSizeIsZero'),
'error'
)
);
}
return false;
}
}
$course_id = api_get_course_id();
//Checking course quota if we are in a course
if (!empty($course_id)) {
$max_filled_space = DocumentManager::get_course_quota();
// Check if there is enough space to save the file
if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
if ($show_output) {
Display::addFlash(
Display::return_message(
get_lang('UplNotEnoughSpace'),
'error'
)
);
}
return false;
}
}
// case 0: default: We assume there is no error, the file uploaded with success.
return true;
}
function sanitizeSvgFile(string $fullPath)
{
$fileType = mime_content_type($fullPath);
if ('image/svg+xml' !== $fileType) {
return;
}
$svgContent = file_get_contents($fullPath);
$sanitizer = new Sanitizer();
$cleanSvg = $sanitizer->sanitize($svgContent);
file_put_contents($fullPath, $cleanSvg);
}
/**
* This function does the save-work for the documents.
* It handles the uploaded file and adds the properties to the database
* If unzip=1 and the file is a zipfile, it is extracted
* If we decide to save ALL kinds of documents in one database,
* we could extend this with a $type='document', 'scormdocument',...
*
* @param array $courseInfo
* @param array $uploadedFile ($_FILES)
* array(
* 'name' => 'picture.jpg',
* 'tmp_name' => '...', // absolute path
* );
* @param string $documentDir Example: /var/www/chamilo/courses/ABC/document
* @param string $uploadPath Example: /folder1/folder2/
* @param int $userId
* @param int $groupId group.id
* @param int $toUserId User ID, or NULL for everybody
* @param int $unzip 1/0
* @param string $whatIfFileExists overwrite, rename or warn if exists (default)
* @param bool $output optional output parameter
* @param bool $onlyUploadFile
* @param string $comment
* @param int $sessionId
* @param bool $treat_spaces_as_hyphens
*
* So far only use for unzip_uploaded_document function.
* If no output wanted on success, set to false.
*
* @return string path of the saved file
*/
function handle_uploaded_document(
$courseInfo,
$uploadedFile,
$documentDir,
$uploadPath,
$userId,
$groupId = 0,
$toUserId = null,
$unzip = 0,
$whatIfFileExists = '',
$output = true,
$onlyUploadFile = false,
$comment = null,
$sessionId = null,
$treat_spaces_as_hyphens = true
) {
if (empty($uploadedFile) || empty($userId) || empty($courseInfo) || empty($documentDir) || empty($uploadPath)) {
return false;
}
$userInfo = api_get_user_info();
$uploadedFile['name'] = stripslashes($uploadedFile['name']);
// Add extension to files without one (if possible)
$uploadedFile['name'] = add_ext_on_mime($uploadedFile['name'], $uploadedFile['type']);
$sessionId = (int) $sessionId;
if (empty($sessionId)) {
$sessionId = api_get_session_id();
}
$groupInfo = [];
if (!empty($groupId)) {
$groupInfo = GroupManager::get_group_properties($groupId);
}
// Just in case process_uploaded_file is not called
$maxSpace = DocumentManager::get_course_quota();
// Check if there is enough space to save the file
if (!DocumentManager::enough_space($uploadedFile['size'], $maxSpace)) {
if ($output) {
Display::addFlash(Display::return_message(get_lang('UplNotEnoughSpace'), 'error'));
}
return false;
}
if (!Security::check_abs_path($documentDir.$uploadPath, $documentDir.'/')) {
Display::addFlash(
Display::return_message(
get_lang('Forbidden'),
'error'
)
);
return false;
}
// If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
return unzip_uploaded_document(
$courseInfo,
$userInfo,
$uploadedFile,
$uploadPath,
$documentDir,
$maxSpace,
$sessionId,
$groupId,
$output,
$onlyUploadFile,
$whatIfFileExists
);
} elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
// We can only unzip ZIP files (no gz, tar,...)
if ($output) {
Display::addFlash(
Display::return_message(get_lang('UplNotAZip')." ".get_lang('PleaseTryAgain'), 'error')
);
}
return false;
} else {
// Clean up the name, only ASCII characters should stay. (and strict)
$cleanName = api_replace_dangerous_char($uploadedFile['name'], $treat_spaces_as_hyphens);
// No "dangerous" files
$cleanName = disable_dangerous_file($cleanName);
// Checking file extension
if (!filter_extension($cleanName)) {
if ($output) {
Display::addFlash(
Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error')
);
}
return false;
} else {
// If the upload path differs from / (= root) it will need a slash at the end
if ($uploadPath !== '/') {
$uploadPath = $uploadPath.'/';
}
// Full path to where we want to store the file with trailing slash
$whereToSave = $documentDir.$uploadPath;
// At least if the directory doesn't exist, tell so
if (!is_dir($whereToSave)) {
if (!mkdir($whereToSave, api_get_permissions_for_new_directories())) {
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('DestDirectoryDoesntExist').' ('.$uploadPath.')',
'error'
)
);
}
return false;
}
}
// Just upload the file "as is"
if ($onlyUploadFile) {
$errorResult = moveUploadedFile($uploadedFile, $whereToSave.$cleanName);
if ($errorResult) {
return $whereToSave.$cleanName;
} else {
return $errorResult;
}
}
/*
Based in the clean name we generate a new filesystem name
Using the session_id and group_id if values are not empty
*/
$fileSystemName = DocumentManager::fixDocumentName(
$cleanName,
'file',
$courseInfo,
$sessionId,
$groupId
);
// Name of the document without the extension (for the title)
$documentTitle = get_document_title($uploadedFile['name']);
// Size of the uploaded file (in bytes)
$fileSize = $uploadedFile['size'];
// File permissions
$filePermissions = api_get_permissions_for_new_files();
// Example: /var/www/chamilo/courses/xxx/document/folder/picture.jpg
$fullPath = $whereToSave.$fileSystemName;
// Example: /folder/picture.jpg
$filePath = $uploadPath.$fileSystemName;
$docId = DocumentManager::get_document_id(
$courseInfo,
$filePath,
$sessionId
);
// What to do if the target file exists
switch ($whatIfFileExists) {
// Overwrite the file if it exists
case 'overwrite':
// Check if the target file exists, so we can give another message
$fileExists = file_exists($fullPath);
if (moveUploadedFile($uploadedFile, $fullPath)) {
sanitizeSvgFile($fullPath);
chmod($fullPath, $filePermissions);
if ($fileExists && $docId) {
// UPDATE DATABASE
$documentId = DocumentManager::get_document_id(
$courseInfo,
$filePath
);
if (is_numeric($documentId)) {
// Update file size
update_existing_document(
$courseInfo,
$documentId,
$uploadedFile['size']
);
// Update document item_property
api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentUpdated',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);
// Redo visibility
api_set_default_visibility(
$documentId,
TOOL_DOCUMENT,
null,
$courseInfo
);
} else {
// There might be cases where the file exists on disk but there is no registration of
// that in the database
// In this case, and if we are in overwrite mode, overwrite and create the db record
$documentId = add_document(
$courseInfo,
$filePath,
'file',
$fileSize,
$documentTitle,
$comment,
0,
true,
$groupId,
$sessionId
);
if ($documentId) {
// Put the document in item_property update
api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentAdded',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);
// Redo visibility
api_set_default_visibility(
$documentId,
TOOL_DOCUMENT,
null,
$courseInfo
);
}
}
// If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message with extra info to user
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br /> '.
$documentTitle.' '.get_lang('UplFileOverwritten'),
'confirmation',
false
)
);
}
return $filePath;
} else {
// Put the document data in the database
$documentId = add_document(
$courseInfo,
$filePath,
'file',
$fileSize,
$documentTitle,
$comment,
0,
true,
$groupId,
$sessionId
);
if ($documentId) {
// Put the document in item_property update
api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentAdded',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);
// Redo visibility
api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo);
}
// If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message to user
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br /> '.$documentTitle,
'confirmation',
false
)
);
}
return $filePath;
}
} else {
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUnableToSaveFile'),
'error',
false
)
);
}
return false;
}
break;
case 'rename':
// Rename the file if it exists
// Always rename.
$cleanName = DocumentManager::getUniqueFileName(
$uploadPath,
$cleanName,
$courseInfo,
$sessionId,
$groupId
);
$fileSystemName = DocumentManager::fixDocumentName(
$cleanName,
'file',
$courseInfo,
$sessionId,
$groupId
);
$documentTitle = disable_dangerous_file($cleanName);
$fullPath = $whereToSave.$fileSystemName;
$filePath = $uploadPath.$fileSystemName;
if (moveUploadedFile($uploadedFile, $fullPath)) {
sanitizeSvgFile($fullPath);
chmod($fullPath, $filePermissions);
// Put the document data in the database
$documentId = add_document(
$courseInfo,
$filePath,
'file',
$fileSize,
$documentTitle,
$comment, // comment
0, // read only
true, // save visibility
$groupId,
$sessionId
);
if ($documentId) {
// Update document item_property
api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentAdded',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);
// Redo visibility
api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo);
}
// If the file is in a folder, we need to update all parent folders
item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message to user
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br />'.
get_lang('UplFileSavedAs').' '.$documentTitle,
'success',
false
)
);
}
return $filePath;
} else {
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUnableToSaveFile'),
'error',
false
)
);
}
return false;
}
break;
case 'nothing':
$fileExists = file_exists($fullPath);
if ($fileExists) {
if ($output) {
Display::addFlash(
Display::return_message(
$uploadPath.$cleanName.' '.get_lang('UplAlreadyExists'),
'warning',
false
)
);
}
break;
}
// no break
default:
// Only save the file if it doesn't exist or warn user if it does exist
if (file_exists($fullPath) && $docId) {
if ($output) {
Display::addFlash(
Display::return_message($cleanName.' '.get_lang('UplAlreadyExists'), 'warning', false)
);
}
} else {
if (moveUploadedFile($uploadedFile, $fullPath)) {
chmod($fullPath, $filePermissions);
// Put the document data in the database
$documentId = add_document(
$courseInfo,
$filePath,
'file',
$fileSize,
$documentTitle,
$comment,
0,
true,
$groupId,
$sessionId
);
if ($documentId) {
// Update document item_property
api_item_property_update(
$courseInfo,
TOOL_DOCUMENT,
$documentId,
'DocumentAdded',
$userId,
$groupInfo,
$toUserId,
null,
null,
$sessionId
);
// Redo visibility
api_set_default_visibility($documentId, TOOL_DOCUMENT, null, $courseInfo);
}
// If the file is in a folder, we need to update all parent folders
item_property_update_on_folder(
$courseInfo,
$uploadPath,
$userId
);
// Display success message to user
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUploadSucceeded').'<br /> '.$documentTitle,
'confirm',
false
)
);
}
return $filePath;
} else {
if ($output) {
Display::addFlash(
Display::return_message(
get_lang('UplUnableToSaveFile'),
'error',
false
)
);
}
return false;
}
}
break;
}
}
}
}
function moveUploadedFile(array $file, string $storePath): bool
{
$handleFromFile = isset($file['from_file']) && $file['from_file'];
$moveFile = isset($file['move_file']) && $file['move_file'];
$copyFile = isset($file['copy_file']) && $file['copy_file'];
if ($moveFile) {
$copied = copy($file['tmp_name'], $storePath);
if (!$copied) {
return false;
}
}
if ($copyFile) {
$copied = copy($file['tmp_name'], $storePath);
unlink($file['tmp_name']);
return $copied;
}
if ($handleFromFile) {
return file_exists($file['tmp_name']);
} else {
return move_uploaded_file($file['tmp_name'], $storePath);
}
}
/**
* Checks if there is enough place to add a file on a directory
* on the base of a maximum directory size allowed
* deprecated: use enough_space instead!
*
* @author Hugues Peeters <peeters@ipm.ucl.ac.be>
*
* @param int $file_size Size of the file in byte
* @param string $dir Path of the directory where the file should be added
* @param int $max_dir_space Maximum size of the diretory in byte
*
* @return bool true if there is enough space, false otherwise
*
* @see enough_size() uses dir_total_space() function
*/
function enough_size($file_size, $dir, $max_dir_space)
{
// If the directory is the archive directory, safely ignore the size limit
if (api_get_path(SYS_ARCHIVE_PATH) == $dir) {
return true;
}
if ($max_dir_space) {
$already_filled_space = dir_total_space($dir);
if (($file_size + $already_filled_space) > $max_dir_space) {
return false;
}
}
return true;
}
/**
* Computes the size already occupied by a directory and is subdirectories.
*
* @author Hugues Peeters <peeters@ipm.ucl.ac.be>
*
* @param string $dir_path Size of the file in byte
*
* @return int Return the directory size in bytes
*/
function dir_total_space($dir_path)
{
$save_dir = getcwd();
chdir($dir_path);
$handle = opendir($dir_path);
$sumSize = 0;
$dirList = [];
while ($element = readdir($handle)) {
if ($element == '.' || $element == '..') {
continue; // Skip the current and parent directories
}
if (is_file($element)) {
$sumSize += filesize($element);
}
if (is_dir($element)) {
$dirList[] = $dir_path.'/'.$element;
}
}
closedir($handle);
if (sizeof($dirList) > 0) {
foreach ($dirList as $j) {
$sizeDir = dir_total_space($j); // Recursivity
$sumSize += $sizeDir;
}
}
chdir($save_dir); // Return to initial position
return $sumSize;
}
/**
* Tries to add an extension to files without extension
* Some applications on Macintosh computers don't add an extension to the files.
* This subroutine try to fix this on the basis of the MIME type sent
* by the browser.
*
* Note : some browsers don't send the MIME Type (e.g. Netscape 4).
* We don't have solution for this kind of situation
*
* @author Hugues Peeters <peeters@ipm.ucl.ac.be>
* @author Bert Vanderkimpen
*
* @param string $file_name Name of the file
* @param string $file_type Type of the file
*
* @return string File name
*/
function add_ext_on_mime($file_name, $file_type)
{
// Check whether the file has an extension AND whether the browser has sent a MIME Type
if (!preg_match('/^.*\.[a-zA-Z_0-9]+$/', $file_name) && $file_type) {
// Build a "MIME-types / extensions" connection table
static $mime_type = [];
$mime_type[] = 'application/msword';
$extension[] = '.doc';
$mime_type[] = 'application/rtf';
$extension[] = '.rtf';
$mime_type[] = 'application/vnd.ms-powerpoint';
$extension[] = '.ppt';
$mime_type[] = 'application/vnd.ms-excel';
$extension[] = '.xls';
$mime_type[] = 'application/pdf';
$extension[] = '.pdf';
$mime_type[] = 'application/postscript';
$extension[] = '.ps';
$mime_type[] = 'application/mac-binhex40';
$extension[] = '.hqx';
$mime_type[] = 'application/x-gzip';
$extension[] = 'tar.gz';
$mime_type[] = 'application/x-shockwave-flash';
$extension[] = '.swf';
$mime_type[] = 'application/x-stuffit';
$extension[] = '.sit';
$mime_type[] = 'application/x-tar';
$extension[] = '.tar';
$mime_type[] = 'application/zip';
$extension[] = '.zip';
$mime_type[] = 'application/x-tar';
$extension[] = '.tar';
$mime_type[] = 'text/html';
$extension[] = '.html';
$mime_type[] = 'text/plain';
$extension[] = '.txt';
$mime_type[] = 'text/rtf';
$extension[] = '.rtf';
$mime_type[] = 'img/gif';
$extension[] = '.gif';
$mime_type[] = 'img/jpeg';
$extension[] = '.jpg';
$mime_type[] = 'img/png';
$extension[] = '.png';
$mime_type[] = 'audio/midi';
$extension[] = '.mid';
$mime_type[] = 'audio/mpeg';
$extension[] = '.mp3';
$mime_type[] = 'audio/x-aiff';
$extension[] = '.aif';
$mime_type[] = 'audio/x-pn-realaudio';
$extension[] = '.rm';
$mime_type[] = 'audio/x-pn-realaudio-plugin';
$extension[] = '.rpm';
$mime_type[] = 'audio/x-wav';
$extension[] = '.wav';
$mime_type[] = 'video/mpeg';
$extension[] = '.mpg';
$mime_type[] = 'video/mpeg4-generic';
$extension[] = '.mp4';
$mime_type[] = 'video/quicktime';
$extension[] = '.mov';
$mime_type[] = 'video/x-msvideo';
$extension[] = '.avi';
$mime_type[] = 'video/x-ms-wmv';
$extension[] = '.wmv';
$mime_type[] = 'video/x-flv';
$extension[] = '.flv';
$mime_type[] = 'image/svg+xml';
$extension[] = '.svg';
$mime_type[] = 'image/svg+xml';
$extension[] = '.svgz';
$mime_type[] = 'video/ogg';
$extension[] = '.ogv';
$mime_type[] = 'audio/ogg';
$extension[] = '.oga';
$mime_type[] = 'application/ogg';
$extension[] = '.ogg';
$mime_type[] = 'application/ogg';
$extension[] = '.ogx';
$mime_type[] = 'application/x-freemind';
$extension[] = '.mm';
$mime_type[] = 'application/vnd.ms-word.document.macroEnabled.12';
$extension[] = '.docm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
$extension[] = '.docx';
$mime_type[] = 'application/vnd.ms-word.template.macroEnabled.12';
$extension[] = '.dotm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
$extension[] = '.dotx';
$mime_type[] = 'application/vnd.ms-powerpoint.template.macroEnabled.12';
$extension[] = '.potm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.template';
$extension[] = '.potx';
$mime_type[] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
$extension[] = '.ppam';
$mime_type[] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
$extension[] = '.ppsm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
$extension[] = '.ppsx';
$mime_type[] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
$extension[] = '.pptm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
$extension[] = '.pptx';
$mime_type[] = 'application/vnd.ms-excel.addin.macroEnabled.12';
$extension[] = '.xlam';
$mime_type[] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
$extension[] = '.xlsb';
$mime_type[] = 'application/vnd.ms-excel.sheet.macroEnabled.12';
$extension[] = '.xlsm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$extension[] = '.xlsx';
$mime_type[] = 'application/vnd.ms-excel.template.macroEnabled.12';
$extension[] = '.xltm';
$mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
$extension[] = '.xltx';
// Test on PC (files with no extension get application/octet-stream)