1
+ <?php
2
+ header ('Content-Type: text/plain; charset=utf-8 ' );
3
+
4
+ $ dir = "../uploads " ;
5
+ $ log = "" ;
6
+
7
+ try {
8
+ // Undefined | Multiple Files | $_FILES Corruption Attack
9
+ // If this request falls under any of them, treat it invalid.
10
+ if (
11
+ !isset ($ _FILES ['upfile ' ]['error ' ]) ||
12
+ is_array ($ _FILES ['upfile ' ]['error ' ])
13
+ ) {
14
+ throw new RuntimeException ('Invalid parameters. ' );
15
+ }
16
+
17
+ // Check $_FILES['upfile']['error'] value.
18
+ switch ($ _FILES ['upfile ' ]['error ' ]) {
19
+ case UPLOAD_ERR_OK :
20
+ break ;
21
+ case UPLOAD_ERR_NO_FILE :
22
+ throw new RuntimeException ('No file sent. ' );
23
+ case UPLOAD_ERR_INI_SIZE :
24
+ case UPLOAD_ERR_FORM_SIZE :
25
+ throw new RuntimeException ('Exceeded filesize limit. ' );
26
+ default :
27
+ throw new RuntimeException ('Unknown errors. ' );
28
+ }
29
+
30
+ // You should also check filesize here.
31
+ if ($ _FILES ['upfile ' ]['size ' ] > 2000000 ) {
32
+ throw new RuntimeException ('Exceeded filesize limit. ' );
33
+ }
34
+
35
+ // DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
36
+ // Check MIME Type by yourself.
37
+ if (false === $ ext = array_search (
38
+ my_mime_content_type ($ _FILES ['upfile ' ]['name ' ]),
39
+ array (
40
+ 'jpg ' => 'image/jpeg ' ,
41
+ 'png ' => 'image/png ' ,
42
+ 'gif ' => 'image/gif ' ,
43
+ 'txt ' => 'text/plain '
44
+ ),
45
+ true
46
+ )) {
47
+ throw new RuntimeException ('Invalid file format. ' );
48
+ }
49
+
50
+ // You should name it uniquely.
51
+ // DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
52
+ // On this example, obtain safe unique name from its binary data.
53
+ if (!move_uploaded_file (
54
+ $ _FILES ['upfile ' ]['tmp_name ' ],
55
+ sprintf ("$ dir/%s.%s " , sha1_file ($ _FILES ['upfile ' ]['tmp_name ' ]), $ ext )
56
+ /*$dir."/".$_FILES['upfile']['name']*/
57
+ )) {
58
+ throw new RuntimeException ('Failed to move uploaded file. ' );
59
+ }
60
+
61
+ echo 'File is uploaded successfully. ' ;
62
+ $ log .= "File is uploaded successfully. \n" ;
63
+ }
64
+ catch (RuntimeException $ e ) {
65
+ echo $ e ->getMessage ();
66
+ $ log .= $ e ->getMessage ()."\n" ;
67
+ }
68
+
69
+ // logging info
70
+ file_put_contents ("$ dir/log.txt " , date ('Y-m-d H:i:s ' )."\n" .$ log , FILE_APPEND | LOCK_EX );
71
+
72
+
73
+ // my wrapper for mime-content-type
74
+ function my_mime_content_type ($ filename ) {
75
+
76
+ $ mime_types = array (
77
+
78
+ 'sql ' => 'text/plain ' ,
79
+
80
+ 'txt ' => 'text/plain ' ,
81
+ 'htm ' => 'text/html ' ,
82
+ 'html ' => 'text/html ' ,
83
+ 'php ' => 'text/html ' ,
84
+ 'css ' => 'text/css ' ,
85
+ 'js ' => 'application/javascript ' ,
86
+ 'json ' => 'application/json ' ,
87
+ 'xml ' => 'application/xml ' ,
88
+ 'swf ' => 'application/x-shockwave-flash ' ,
89
+ 'flv ' => 'video/x-flv ' ,
90
+
91
+ // images
92
+ 'png ' => 'image/png ' ,
93
+ 'jpe ' => 'image/jpeg ' ,
94
+ 'jpeg ' => 'image/jpeg ' ,
95
+ 'jpg ' => 'image/jpeg ' ,
96
+ 'gif ' => 'image/gif ' ,
97
+ 'bmp ' => 'image/bmp ' ,
98
+ 'ico ' => 'image/vnd.microsoft.icon ' ,
99
+ 'tiff ' => 'image/tiff ' ,
100
+ 'tif ' => 'image/tiff ' ,
101
+ 'svg ' => 'image/svg+xml ' ,
102
+ 'svgz ' => 'image/svg+xml ' ,
103
+
104
+ // archives
105
+ 'zip ' => 'application/zip ' ,
106
+ 'rar ' => 'application/x-rar-compressed ' ,
107
+ 'exe ' => 'application/x-msdownload ' ,
108
+ 'msi ' => 'application/x-msdownload ' ,
109
+ 'cab ' => 'application/vnd.ms-cab-compressed ' ,
110
+
111
+ // audio/video
112
+ 'mp3 ' => 'audio/mpeg ' ,
113
+ 'qt ' => 'video/quicktime ' ,
114
+ 'mov ' => 'video/quicktime ' ,
115
+
116
+ // adobe
117
+ 'pdf ' => 'application/pdf ' ,
118
+ 'psd ' => 'image/vnd.adobe.photoshop ' ,
119
+ 'ai ' => 'application/postscript ' ,
120
+ 'eps ' => 'application/postscript ' ,
121
+ 'ps ' => 'application/postscript ' ,
122
+
123
+ // ms office
124
+ 'doc ' => 'application/msword ' ,
125
+ 'rtf ' => 'application/rtf ' ,
126
+ 'xls ' => 'application/vnd.ms-excel ' ,
127
+ 'ppt ' => 'application/vnd.ms-powerpoint ' ,
128
+
129
+ // open office
130
+ 'odt ' => 'application/vnd.oasis.opendocument.text ' ,
131
+ 'ods ' => 'application/vnd.oasis.opendocument.spreadsheet ' ,
132
+ );
133
+
134
+ $ ext = strtolower (array_pop (explode ('. ' ,$ filename )));
135
+ if (array_key_exists ($ ext , $ mime_types )) {
136
+ return $ mime_types [$ ext ];
137
+ }
138
+ elseif (function_exists ('finfo_open ' )) {
139
+ $ finfo = finfo_open (FILEINFO_MIME );
140
+ $ mimetype = finfo_file ($ finfo , $ filename );
141
+ finfo_close ($ finfo );
142
+ return $ mimetype ;
143
+ }
144
+ else {
145
+ return 'application/octet-stream ' ;
146
+ }
147
+ }
148
+
149
+ ?>
0 commit comments