Test Failed
Push — develop ( fe93c2...a526c9 )
by nguereza
05:50
created

Upload::dirExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 4
rs 10
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    /**
32
     *    Upload
33
     *
34
     *    A complete class to upload files with php 5 or higher, but the best: very simple to use.
35
     *
36
     *    @author Olaf Erlandsen <[email protected]>
37
     *    @author http://www.webdevfreelance.com/
38
     *
39
     *    @package FileUpload
40
     *    @version 1.5
41
     */
42
    class Upload extends BaseClass {
43
44
        /**
45
         *   Version
46
         *
47
         *   @since      1.5
48
         *   @version    1.0
49
         */
50
        const VERSION = '1.5';
51
52
        /**
53
         *    Upload function name
54
         *    Remember:
55
         *        Default function: move_uploaded_file
56
         *        Native options:
57
         *            - move_uploaded_file (Default and best option)
58
         *            - copy
59
         *
60
         *    @since        1.0
61
         *    @version    1.0
62
         *    @var        string
63
         */
64
        private $uploadFunction = 'move_uploaded_file';
65
66
        /**
67
         *    Array with the information obtained from the
68
         *    variable $_FILES or $HTTP_POST_FILES.
69
         *
70
         *    @since        1.0
71
         *    @version    1.0
72
         *    @var        array
73
         */
74
        private $uploadedFileData = array();
75
76
        /**
77
         *    If the file you are trying to upload already exists it will
78
         *    be overwritten if you set the variable to true.
79
         *
80
         *    @since        1.0
81
         *    @version    1.0
82
         *    @var        boolean
83
         */
84
        private $overwriteFile = false;
85
86
        /**
87
         *    Input element
88
         *    Example:
89
         *        <input type="file" name="file" />
90
         *    Result:
91
         *        FileUpload::$input = file
92
         *
93
         *    @since        1.0
94
         *    @version    1.0
95
         *    @var        string
96
         */
97
        private $input;
98
99
        /**
100
         *    Path output
101
         *
102
         *    @since        1.0
103
         *    @version    1.0
104
         *    @var        string
105
         */
106
        private $destinationDirectory;
107
108
        /**
109
         *    Output filename
110
         *
111
         *    @since        1.0
112
         *    @version    1.0
113
         *    @var        string
114
         */
115
        private $filename;
116
117
        /**
118
         *    Max file size
119
         *
120
         *    @since        1.0
121
         *    @version    1.0
122
         *    @var        float
123
         */
124
        private $maxFileSize = 0.0;
125
126
        /**
127
         *    List of allowed mime types
128
         *
129
         *    @since        1.0
130
         *    @version    1.0
131
         *    @var        array
132
         */
133
        private $allowedMimeTypes = array();
134
135
        /**
136
         *    Callbacks
137
         *
138
         *    @since        1.0
139
         *    @version    1.0
140
         *    @var        array
141
         */
142
        private $callbacks = array();
143
144
        /**
145
         *    File object
146
         *
147
         *    @since        1.0
148
         *    @version    1.0
149
         *    @var        object
150
         */
151
        private $file;
152
153
        /**
154
         *    Helping mime types
155
         *
156
         *    @since        1.0
157
         *    @version    1.0
158
         *    @var        array
159
         */
160
        private $mimeHelping = array(
161
            'text'      =>    array('text/plain',),
162
            'image'     =>    array(
163
                'image/jpeg',
164
                'image/jpg',
165
                'image/pjpeg',
166
                'image/png',
167
                'image/gif',
168
            ),
169
            'document'  =>    array(
170
                'application/pdf',
171
                'application/msword',
172
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
173
                'application/vnd.openxmlformats-officedocument.presentationml.presentation',
174
                'application/vnd.ms-powerpoint',
175
                'application/vnd.ms-excel',
176
                'application/vnd.oasis.opendocument.spreadsheet',
177
                'application/vnd.oasis.opendocument.presentation',
178
            ),
179
            'video'    =>    array(
180
                'video/3gpp',
181
                'video/3gpp',
182
                'video/x-msvideo',
183
                'video/avi',
184
                'video/mpeg4',
185
                'video/mp4',
186
                'video/mpeg',
187
                'video/mpg',
188
                'video/quicktime',
189
                'video/x-sgi-movie',
190
                'video/x-ms-wmv',
191
                'video/x-flv',
192
            ),
193
        );
194
195
        /**
196
         * The loaded translations errors messages
197
         * @var array
198
         */
199
        private $errorMessages = array();
200
201
        /**
202
         * The upload error message
203
         * @var string
204
         */
205
        private $error = null;
206
207
208
        /**
209
         *    Construct
210
         *
211
         *    @since     0.1
212
         *    @version   1.0.1
213
         *    @return    object
214
         *    @method    object    __construct
215
         */
216
        public function __construct() {
217
            parent::__construct();
218
219
            //Load language messages
220
            $this->loadLangMessages();
221
222
            $this->file = array(
223
                'status'                =>    false, // True: success upload
224
                'mime'                  =>    '', // Empty string
225
                'filename'              =>    '', // Empty string
226
                'original'              =>    '', // Empty string
227
                'size'                  =>    0, // 0 Bytes
228
                'sizeFormated'          =>    '0B', // 0 Bytes
229
                'destination'           =>    './', // Default: ./
230
                'allowed_mime_types'    =>    array(), // Allowed mime types
231
                'error'                 =>    null, // File error
232
            );
233
234
            // Change dir to current dir
235
            $this->destinationDirectory = dirname(__FILE__) . DIRECTORY_SEPARATOR;
236
237
            // Set file array
238
            $this->uploadedFileData = get_instance()->globalvar->files();
239
            $this->logger->info('The upload file information are : ' . stringify_vars($this->uploadedFileData));
240
        }
241
242
        
243
        /**
244
         *    Set input.
245
         *    If you have $_FILES["file"], you must use the key "file"
246
         *    Example:
247
         *        $object->setInput("file");
248
         *
249
         *    @since     1.0
250
         *    @version   1.0
251
         *    @param     string      $input
252
         *    @return    object
253
         *    @method    boolean     setInput
254
         */
255
        public function setInput($input) {
256
            $this->input = $input;
257
            return $this;
258
        }
259
260
        
261
        /**
262
         *    Set new filename
263
         *    Example:
264
         *        FileUpload::setFilename("new file.txt")
265
         *    Remember:
266
         *        Use %s to retrive file extension
267
         *
268
         *    @since     1.0
269
         *    @version   1.0
270
         *    @param     string      $filename
271
         *    @return    object
272
         *    @method    boolean     setFilename
273
         */
274
        public function setFilename($filename) {
275
            if ($this->isFilename($filename)) {
276
                $this->filename = $filename;
277
            }
278
            return $this;
279
        }
280
281
        /**
282
         *    Set automatic filename
283
         *
284
         *    @since     1.0
285
         *    @version   1.5
286
         *    @param     string      $extension
287
         *    @return    object
288
         *    @method    boolean     setAutoFilename
289
         */
290
        public function setAutoFilename() {
291
            $this->filename = sha1(mt_rand(1, 9999) . uniqid()) . time();
292
            return $this;
293
        }
294
295
        /**
296
         *    Set file size limit
297
         *
298
         *    @since     1.0
299
         *    @version   1.0
300
         *    @param     double     $fileSize
301
         *    @return    object
302
         *    @method    boolean     setMaxFileSize
303
         */
304
        public function setMaxFileSize($fileSize) {
305
            $size = $this->sizeInBytes($fileSize);
306
            if (is_numeric($size) && $size > -1) {
307
                // Get PHP upload max file size config
308
                $phpMaxUploadSize = ini_get('upload_max_filesize');
309
                // check difference
310
                if ($this->sizeInBytes((int) $phpMaxUploadSize) < $size) {
311
                    $this->logger->warning('The upload max file size you set [' . $fileSize . '] '
312
                            . 'is greather than the PHP configuration for upload max file size [' . $phpMaxUploadSize . ']');
313
                }
314
                $this->maxFileSize = $size;
315
            }
316
            return $this;
317
        }
318
319
         /**
320
         *    Append a mime type to allowed mime types
321
         *
322
         *    @since     1.0
323
         *    @version   1.0.1
324
         *    @param     string      $mime
325
         *    @return    object
326
         *    @method    boolean     setAllowMimeType
327
         */
328
        public function setAllowMimeType($mime) {
329
            $this->allowedMimeTypes[] = strtolower($mime);
330
            $this->file['allowed_mime_types'][] = strtolower($mime); 
331
            return $this;
332
        }
333
334
        /**
335
         *    Set array mime types
336
         *
337
         *    @since     1.0
338
         *    @version   1.0
339
         *    @param     array       $mimes
340
         *    @return    object
341
         *    @method    boolean     setAllowMimeTypes
342
         */
343
        public function setAllowMimeTypes(array $mimes) {
344
            array_map(array($this, 'setAllowMimeType'), $mimes);
345
            return $this;
346
        }
347
348
        /**
349
         *    Set allowed mime types from mime helping
350
         *
351
         *    @since     1.0.1
352
         *    @version   1.0.1
353
         *    @return    object
354
         *    @method    boolean    setMimeHelping
355
         */
356
        public function setMimeHelping($name) {
357
            if (array_key_exists($name, $this->mimeHelping)) {
358
                return $this->setAllowMimeTypes($this->mimeHelping[$name]);
359
            }
360
            return $this;
361
        }
362
363
        /**
364
         *    Clear allowed mime types cache
365
         *
366
         *    @since     1.0
367
         *    @version   1.0
368
         *    @return    object
369
         *    @method    boolean    clearAllowedMimeTypes
370
         */
371
        public function clearAllowedMimeTypes() {
372
            $this->allowedMimeTypes = array();
373
            $this->file['allowed_mime_types'] = array();
374
            return $this;
375
        }
376
377
        /**
378
         *    Set input callback
379
         *
380
         *    @since     1.0
381
         *    @version   1.0
382
         *    @param     mixed       $callback
383
         *    @return    object
384
         *    @method    boolean     setCallbackInput
385
         */
386
        public function setCallbackInput($callback) {
387
            if (is_callable($callback, false)) {
388
                $this->callbacks['input'] = $callback;
389
            }
390
            return $this;
391
        }
392
393
        /**
394
         *    Set output callback
395
         *
396
         *    @since     1.0
397
         *    @version   1.0
398
         *    @param     mixed       $callback
399
         *    @return    object
400
         *    @method    boolean     setCallbackOutput
401
         */
402
        public function setCallbackOutput($callback) {
403
            if (is_callable($callback, false)) {
404
                $this->callbacks['output'] = $callback;
405
            }
406
            return $this;
407
        }
408
409
        /**
410
         *    Set function to upload file
411
         *    Examples:
412
         *        1.- FileUpload::setUploadFunction("move_uploaded_file");
413
         *        2.- FileUpload::setUploadFunction("copy");
414
         *
415
         *    @since     1.0
416
         *    @version   1.0
417
         *    @param     string      $function
418
         *    @return    object
419
         *    @method    boolean     setUploadFunction
420
         */
421
        public function setUploadFunction($function) {
422
            if (is_callable($function)) {
423
                $this->uploadFunction = $function;
424
            }
425
            return $this;
426
        }
427
428
        /**
429
         *    Allow overwriting files
430
         *
431
         *    @since      1.0
432
         *    @version    1.0
433
         *    @return     object
434
         *    @method     boolean    allowOverwriting
435
         */
436
        public function allowOverwriting($status = true) {
437
            $this->overwriteFile = $status;
438
            return $this;
439
        }
440
441
         /**
442
         *    Get the allow overwriting
443
         *    @return    boolean
444
         */
445
        public function isAllowOverwriting() {
446
            return $this->overwriteFile ;
447
        }
448
449
        /**
450
         *    Set destination output
451
         *
452
         *    @since     1.0
453
         *    @version   1.0
454
         *    @param     string      $directory      Destination path
455
         *    @return    object
456
         *    @method    boolean     setDestinationDirectory
457
         */
458
        public function setDestinationDirectory($directory) {
459
            $dir = realpath($directory);
460
            $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
461
            if ($this->isDirpath($dir)) {
462
                if ($this->dirExists($dir)) {
463
                    $this->destinationDirectory = $dir;
464
                    chdir($dir);
465
                } else {
466
                    $this->logger->warning('The upload directory [' . $directory . '] does not exist');
467
                }
468
            }
469
            return $this;
470
        }
471
472
        /**
473
         * Check if the file is uploaded
474
         * @return boolean
475
         */
476
        public function isUploaded() {
477
            return isset($this->uploadedFileData[$this->input])
478
                        && is_uploaded_file($this->uploadedFileData[$this->input]['tmp_name']);
479
        }
480
481
        /**
482
         *    Upload file
483
         *
484
         *    @since     1.0
485
         *    @version   1.0.1
486
         *    @return    boolean
487
         *    @method    boolean    save
488
         */
489
        public function save() {
490
            if (!$this->isUploaded()) {
491
                return false;
492
            }
493
            // set original filename if not have a new name
494
            $this->setFilenameUsingUploadedData();
495
496
            // set file info
497
            $this->file['mime']         = $this->uploadedFileData[$this->input]['type'];
498
            $this->file['tmp']          = $this->uploadedFileData[$this->input]['tmp_name'];
499
            $this->file['original']     = $this->uploadedFileData[$this->input]['name'];
500
            $this->file['size']         = $this->uploadedFileData[$this->input]['size'];
501
            $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']);
502
            $this->file['destination']  = $this->destinationDirectory . $this->filename;
503
            $this->file['filename']     = $this->filename;
504
            $this->file['error']        = $this->uploadedFileData[$this->input]['error'];
505
506
            $this->logger->info('The upload file information to process is : ' . stringify_vars($this->file));
507
508
            if ($this->uploadHasError()) {
509
                return false;
510
            }
511
            // Execute input callback
512
            $this->runCallback('input');
513
514
            $this->file['status'] = call_user_func_array(
515
                $this->uploadFunction, array(
516
                    $this->uploadedFileData[$this->input]['tmp_name'],
517
                    $this->destinationDirectory . $this->filename
518
                )
519
            );
520
521
            // Execute output callback
522
            $this->runCallback('output');
523
524
            return $this->file['status'];
525
        }
526
527
        /**
528
         * Get the upload error message
529
         * @return string
530
         */
531
        public function getError() {
532
            return $this->error;
533
        }
534
535
         /**
536
         *    Retrive status of upload
537
         *
538
         *    @since     1.0
539
         *    @version   1.0
540
         *    @return    boolean
541
         *    @method    boolean    getStatus
542
         */
543
        public function getStatus() {
544
            return $this->file['status'];
545
        }
546
547
        /**
548
         *    File info
549
         *
550
         *    @since      1.0
551
         *    @version    1.0
552
         *    @return     object
553
         *    @method     object    getInfo
554
         */
555
        public function getInfo() {
556
            return (object) $this->file;
557
        }
558
559
560
        /**
561
         *    Check file exists
562
         *
563
         *    @since      1.0
564
         *    @version    1.0.1
565
         *    @param      string     $file
566
         *    @return     boolean
567
         *    @method     boolean    fileExists
568
         */
569
        protected function fileExists($file) {
570
            return $this->isFilename($file) 
571
                                && file_exists($file) 
572
                                && is_file($file);
573
        }
574
575
         /**
576
         * Set the filename if is empty using the uploaded data information
577
         *
578
         * @return object the current instance
579
         */
580
        protected function setFilenameUsingUploadedData() {
581
            // set original filename if not have a new name
582
            if (empty($this->filename)) {
583
                $this->filename = $this->uploadedFileData[$this->input]['name'];
584
            } else {
585
                // Replace %s for extension in filename
586
                // Before: /[\w\d]*(.[\d\w]+)$/i
587
                // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu
588
                // Support unicode(utf-8) characters
589
                // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" 
590
                // is valid; "Tønsberg.jpeg" is valid
591
                $extension = preg_replace(
592
                    '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu',
593
                    '$1',
594
                    $this->uploadedFileData[$this->input]['name']
595
                );
596
                $this->filename = $this->filename . '.' . $extension;
597
            }
598
            return $this;
599
        }
600
601
        /**
602
         *    Check dir exists
603
         *
604
         *    @since        1.0
605
         *    @version    1.0.1
606
         *    @param      string     $path
607
         *    @return     boolean
608
         *    @method     boolean    dirExists
609
         */
610
        protected function dirExists($path) {
611
            return $this->isDirpath($path) 
612
                                && file_exists($path) 
613
                                && is_dir($path);
614
        }
615
616
        /**
617
         *    Check valid filename
618
         *
619
         *    @since     1.0
620
         *    @version   1.0.1
621
         *    @param     string      $filename
622
         *    @return    boolean
623
         *    @method    boolean     isFilename
624
         */
625
        protected function isFilename($filename) {
626
            $filename = basename($filename);
627
            return !empty($filename);
628
        }
629
630
        /**
631
         *    Validate mime type with allowed mime types,
632
         *    but if allowed mime types is empty, this method return true
633
         *
634
         *    @since     1.0
635
         *    @version   1.0
636
         *    @param     string      $mime
637
         *    @return    boolean
638
         *    @method    boolean     checkMimeType
639
         */
640
        protected function checkMimeType($mime) {
641
            if (count($this->allowedMimeTypes) === 0) {
642
                return true;
643
            }
644
            return in_array(strtolower($mime), $this->allowedMimeTypes);
645
        }
646
647
        /**
648
         *    Validate max upload file size,
649
         *    but if max size is 0, this method return true
650
         *
651
         *    @since     1.0
652
         *    @version   1.0
653
         *    @param     double|integer      $size
654
         *    @return    boolean
655
         */
656
        protected function checkMaxSize($size) {
657
            if ($this->maxFileSize <= 0) {
658
                return true;
659
            }
660
            return $this->maxFileSize >= $size;
661
        }
662
663
         /**
664
         *    Check the file overwritting
665
         *    @since     1.0
666
         *    @version   1.0
667
         *    @return    boolean
668
         */
669
        protected function checkFileOverwritting() {
670
            if ($this->fileExists($this->destinationDirectory . $this->filename)) {
671
                return $this->overwriteFile;
672
            }
673
            return true;
674
        }
675
       
676
        /**
677
         *    Check valid path
678
         *
679
         *    @since        1.0
680
         *    @version    1.0.1
681
         *    @param        string    $filename
682
         *    @return     boolean
683
         *    @method     boolean    isDirpath
684
         */
685
        protected function isDirpath($path) {
686
            if (DIRECTORY_SEPARATOR == '/') {
687
                return (preg_match('/^[^*?"<>|:]*$/', $path) == 1);
688
            }
689
            return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1);
690
        }
691
692
        /**
693
         *    File size for humans.
694
         *
695
         *    @since      1.0
696
         *    @version    1.0
697
         *    @param      integer    $bytes
698
         *    @param      integer    $precision
699
         *    @return     string
700
         *    @method     string     sizeFormat
701
         */
702
        protected function sizeFormat($size, $precision = 2) {
703
            if ($size > 0) {
704
                $base     = log($size) / log(1024);
705
                $suffixes = array('B', 'K', 'M', 'G', 'T');
706
                $suffixe = '';
707
                if (isset($suffixes[floor($base)])) {
708
                    $suffixe = $suffixes[floor($base)];
709
                }
710
                return round(pow(1024, $base - floor($base)), $precision) . $suffixe;
711
            }
712
            return null;
713
        }
714
715
        
716
        /**
717
         *    Convert human file size to bytes
718
         *
719
         *    @since      1.0
720
         *    @version    1.0.1
721
         *    @param      integer|double    $size
722
         *    @return     integer|double
723
         *    @method     string     sizeInBytes
724
         */
725
        protected function sizeInBytes($size) {
726
            $unit = 'B';
727
            $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4);
728
            $matches = array();
729
            preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches);
730
            if (array_key_exists('unit', $matches)) {
731
                $unit = strtoupper($matches['unit']);
732
            }
733
            return (floatval($matches['size']) * pow(1024, $units[$unit]));
734
        }
735
736
        /**
737
         * Set the upload error message
738
         * @param string $message the upload error message to set
739
         *
740
         * @return object the current instance
741
         */
742
        protected function setError($message) {
743
            $this->logger->error('The file upload got error : ' . $message);
744
            $this->error = $message;
745
            return $this;
746
        }
747
748
        /**
749
         * Run the callbacks in the file uploaded
750
         * @param string $type the type of callback "input" or "output"
751
         * @return void 
752
         */
753
        protected function runCallback($type) {
754
            if (!empty($this->callbacks[$type])) {
755
                call_user_func($this->callbacks[$type], (object) $this->file);
756
            }
757
        }
758
759
        /**
760
         * Check if file upload has error
761
         * @return boolean
762
         */
763
        protected function uploadHasError() {
764
            //check if file upload is  allowed in the configuration
765
            if (!ini_get('file_uploads')) {
766
                $this->setError($this->errorMessages['file_uploads']);
767
                return true;
768
            }
769
770
            //check for php upload error
771
            $error = $this->getPhpUploadErrorMessageByCode($this->file['error']);
772
            if ($error) {
773
                $this->setError($error);
774
                return true;
775
            }
776
            
777
            //check for mime type
778
            if (!$this->checkMimeType($this->file['mime'])) {
779
                $this->setError($this->errorMessages['accept_file_types']);
780
                return true;
781
            }
782
783
            // Check file size
784
            if (!$this->checkMaxSize($this->file['size'])) {
785
                $this->setError(sprintf($this->errorMessages['max_file_size'], $this->sizeFormat($this->maxFileSize)));
786
                return true;
787
            }
788
789
            // Check if exists file
790
            if (!$this->checkFileOverwritting()) {
791
                $this->setError($this->errorMessages['overwritten_not_allowed']);
792
                return true;
793
            }
794
            return false;
795
        }
796
797
        /**
798
         * Get the PHP upload error message for the given code
799
         * @param  int $code the error code
800
         * @return string the error message
801
         */
802
        protected function getPhpUploadErrorMessageByCode($code) {
803
            $error = null;
804
            $codeMessageMaps = array(
805
                1 => $this->errorMessages['upload_err_ini_size'],
806
                2 => $this->errorMessages['upload_err_form_size'],
807
                3 => $this->errorMessages['upload_err_partial'],
808
                4 => $this->errorMessages['upload_err_no_file'],
809
                6 => $this->errorMessages['upload_err_no_tmp_dir'],
810
                7 => $this->errorMessages['upload_err_cant_write'],
811
                8 => $this->errorMessages['upload_err_extension'],
812
            );
813
            if (isset($codeMessageMaps[$code])) {
814
                $error = $codeMessageMaps[$code];
815
            }
816
            return $error;
817
        }
818
819
        /**
820
         * Load the language messages for upload
821
         */
822
        protected function loadLangMessages() {
823
            get_instance()->loader->lang('file_upload');
824
            $this->errorMessages = array(
825
                                    'upload_err_ini_size'     => get_instance()->lang->get('fu_upload_err_ini_size'),
826
                                    'upload_err_form_size'    => get_instance()->lang->get('fu_upload_err_form_size'),
827
                                    'upload_err_partial'      => get_instance()->lang->get('fu_upload_err_partial'),
828
                                    'upload_err_no_file'      => get_instance()->lang->get('fu_upload_err_no_file'),
829
                                    'upload_err_no_tmp_dir'   => get_instance()->lang->get('fu_upload_err_no_tmp_dir'),
830
                                    'upload_err_cant_write'   => get_instance()->lang->get('fu_upload_err_cant_write'),
831
                                    'upload_err_extension'    => get_instance()->lang->get('fu_upload_err_extension'),
832
                                    'accept_file_types'       => get_instance()->lang->get('fu_accept_file_types'),
833
                                    'file_uploads'            => get_instance()->lang->get('fu_file_uploads_disabled'),
834
                                    'max_file_size'           => get_instance()->lang->get('fu_max_file_size'),
835
                                    'overwritten_not_allowed' => get_instance()->lang->get('fu_overwritten_not_allowed'),
836
                                );
837
        }
838
839
    }
840