]>
Commit | Line | Data |
---|---|---|
d8c46ff7 GJ |
1 | <?php |
2 | ||
3 | /** | |
4 | * Returns a human readable error description for file upload errors. | |
5 | * | |
6 | * @author Dan Brown <danbrown@php.net> | |
7 | * @author Michiel Thalen | |
8 | * @copyright Copyright © 1997 - 2016 by the PHP Documentation Group | |
9 | * @license | |
10 | * UploadException is licensed under a Creative Commons Attribution 3.0 License | |
11 | * or later. | |
12 | * | |
13 | * Based on a work at | |
14 | * https://secure.php.net/manual/en/features.file-upload.errors.php#89374. | |
15 | * | |
16 | * You should have received a copy of the Creative Commons Attribution 3.0 | |
17 | * License with this program. If not, see | |
18 | * <https://creativecommons.org/licenses/by/3.0/>. | |
19 | */ | |
20 | ||
21 | ||
22 | class UploadException extends Exception | |
23 | { | |
24 | public function __construct($code) | |
25 | { | |
26 | $message = $this->codeToMessage($code); | |
27 | parent::__construct($message, 500); | |
28 | } | |
29 | ||
30 | private function codeToMessage($code) | |
31 | { | |
32 | switch ($code) { | |
33 | case UPLOAD_ERR_INI_SIZE: | |
34 | $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; | |
35 | break; | |
36 | case UPLOAD_ERR_FORM_SIZE: | |
37 | $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '. | |
38 | 'specified in the HTML form'; | |
39 | break; | |
40 | case UPLOAD_ERR_PARTIAL: | |
41 | $message = 'The uploaded file was only partially uploaded'; | |
42 | break; | |
43 | case UPLOAD_ERR_NO_FILE: | |
44 | $message = 'No file was uploaded'; | |
45 | break; | |
46 | case UPLOAD_ERR_NO_TMP_DIR: | |
47 | $message = 'Missing a temporary folder'; | |
48 | break; | |
49 | case UPLOAD_ERR_CANT_WRITE: | |
50 | $message = 'Failed to write file to disk'; | |
51 | break; | |
52 | case UPLOAD_ERR_EXTENSION: | |
53 | $message = 'File upload stopped by extension'; | |
54 | break; | |
55 | ||
56 | default: | |
57 | $message = 'Unknown upload error'; | |
58 | break; | |
59 | } | |
60 | ||
61 | return $message; | |
62 | } | |
63 | } |