-
Notifications
You must be signed in to change notification settings - Fork 4
/
upload.class.php
321 lines (261 loc) · 10.1 KB
/
upload.class.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
<?php
/**
* This will allow easy handling of a php upload
* @author: Mihai Ionut Vilcu ([email protected])
* 2-July-2013
*/
class Upload
{
var $settings = array(
'folder' => '.', // the folder where the images will be placed
'isImage' => 0, // if true it will treat files as images
'maxSize' => 20, // the max allowed size in MB
'allowed_extensions' => array(), // an array of lowercase allowed extensions, (!) IF EMPTY ALL ARE ALLOWED (!)
'overwrite' => 1, // if true it will overwrite the file on the server in case it has the same name
'custom_names' => false // an array of custom names, it will be handeled circullary, if the array ends but there are files to be uploaded it will start from the top
);
var $errors = array(); // will hold the errors
var $success = array(); // will hold the success messages
var $allowed_chars = "a-z0-9_.-"; // allowed chars in a file name, case insensitive
function __construct($settings = array()) {
// we update the settings
$this->updateSettings($settings);
}
/**
* Will process the files
* @param string $inputName the name of the input to be checked
* @param array $settings settings
* @return array/string uploaded file(s) name
*/
function upload($inputName = 'file', $settings = array()) {
// we update the settings
$this->updateSettings($settings);
if(!isset($_FILES[$inputName])) // if we have no file we have nothing to do
return false;
if(is_array($inputName) || is_object($inputName)) { // multiple input names
$result = array();
foreach ($inputName as $file)
$result[] = $this->handleFiles($file);
return $result;
} else { // single input name
return $this->handleFiles($_FILES[$inputName]);
}
}
/**
* Will handle the files and perform validations
* @param array $files the array of the files from $_FILES
* @return array array with the uploaded files
*/
function handleFiles($files) {
$files = $this->reArrayFiles($files);
if(!is_writable($this->settings['folder'])) {
$this->errors[] = array($this->settings['folder'], " This folder is not writable !");
return false;
}
$result = array();
$i = 0;
foreach ($files as $file) {
$file['name'] = $this->filterFilename($file['name']);
// if no filename nothing to do
if(trim($file['name']) == '')
continue;
if($file['error'] > 0) {
$this->errors[] = array($file['name'], $this->codeToMessage($file['error']));
continue;
}
// we check the file size
if($file['size'] > $this->settings['maxSize'] * 1024 * 1024) {
$this->errors[] = array($file['name'], "The size of the file exceeds the allowed limit (".$this->settings['maxSize']."MB).");
continue;
}
// check the extension, remember if settings allowed_extensions is empty it will return allow all of them
$info = pathinfo($file['name']);
$info['extension'] = isset($info['extension']) ? $info['extension'] : ''; // in case the file name has no extension
if(!empty($this->settings['allowed_extensions']) && !in_array(strtolower($info['extension']), $this->settings['allowed_extensions'])) {
$this->errors[] = array($file['name'], "This extension is not allowed !");
continue;
}
// we build the path for upload
if(!empty($this->settings['custom_names'])) {
// keep it circular
if($i == count($this->settings['custom_names']))
$i = 0;
$upload_path = rtrim($this->settings['folder'], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->settings['custom_names'][$i++];
}
else
$upload_path = rtrim($this->settings['folder'], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file['name'];
// check if the file exists on the server
if(!$this->settings['overwrite'] && file_exists($upload_path)){
$this->errors[] = array($file['name'], "This file already exists, rename it !");
continue;
}
if($this->settings['isImage']) { // we need to handle it as an image
if($img = $this->imagecreatefromfile($file['tmp_name'], $function)) {
if($function($img, $upload_path)) { // we pass the image through a filter
$this->success[] = array($file['name'], "It was uploaded successfully !");
$result[] = $file['name'];
}
// remove uploaded file
@unlink($file['tmp_name']);
} else
$this->errors[] = array($file['name'], "This file is not a valid image !");
} else { // we treat it as a normal file
if(move_uploaded_file($file['tmp_name'], $upload_path)) {
$this->success[] = array($file['name'], "It was uploaded successfully !");
$result[] = $file['name'];
}
}
}
return $result;
}
/**
* it will rearrange the array with the info about the files generated in $_FILES
* @author: http://www.php.net/manual/en/features.file-upload.multiple.php#53240
* @edited: Mihai Ionut Vilcu (it will handle one file also)
* @param array $file_post the $_FILES array
* @return array the new array
*/
function reArrayFiles(&$file_post) {
$file_ary = array();
if(!is_array($file_post['name']))
return array($file_post);
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
/**
* generates the html code for a basic upload form, it can generate the input fields only or the compleate form
* @param integer $number the number of inputs
* @param string $name the name of the input(s)
* @param integer $complete_form if true it will generate the compleate forms insetead of just input fields
* @param string $location location where the form will send the data(in case the form is compleate)
* @param array $extra extra attributes for input(s)
* @param array $extra_form extra attributes for form
* @return string html code generated
*/
function generateInput($number = 1, $name = 'file', $complete_form = 0, $location = '?', $extra = array(), $extra_form = array()) {
$html = $attr = $attr_form = "";
foreach ($extra as $key => $value)
$attr .= " $key = '$value' ";
foreach ($extra_form as $key => $value)
$attr_form .= " $key = '$value' ";
for($i = 0; $i < $number; $i++)
$html .= "File ".($i+1)."
<input type='file' name='$name".($number > 1 ? "[]" : "")."'$attr>
<br/>
";
if($complete_form == 1)
$html = "<form action='$location' method='post' enctype='multipart/form-data' $attr_form>
".$html."
<input type='submit' value='Upload'>\n</form>";
return $html;
}
/**
* gets the max file size for upload allowed on the server in MB
* @return integer the max size in MB
*/
function getMaxUpload() {
$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
return min($max_upload, $max_post, $memory_limit);
}
/**
* makes sure that the file name only contains allowed chars
* @param string $filename file name
* @return string filtered file name
*/
function filterFilename($filename) {
return preg_replace("/[^$this->allowed_chars]/i", "_", $filename);
}
/**
* it will interpret the file upload error codes
* @param integer $code the error code
* @return string error message
*/
function codeToMessage($code) {
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
break;
case UPLOAD_ERR_PARTIAL:
$message = "The uploaded file was only partially uploaded";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = "Missing a temporary folder";
break;
case UPLOAD_ERR_CANT_WRITE:
$message = "Failed to write file to disk";
break;
case UPLOAD_ERR_EXTENSION:
$message = "File upload stopped by extension";
break;
default:
$message = "Unknown upload error";
break;
}
return $message;
}
/**
* makes sure that the settings are updated and correct
* @param array $settings new settings
* @return void
*/
function updateSettings($settings) {
$this->settings = array_merge($this->settings, $settings);
$this->settings['maxSize'] = min($this->settings['maxSize'], $this->getMaxUpload());
}
/**
* will create an image from a file
* @credits: http://www.php.net/manual/en/function.imagecreate.php#81831
* @edited: Mihai Ionut Vilcu ([email protected]) - added $fun
* @param string $path path to the file
* @param string $fun it will hold the function required for adding image data in the file
* @param boolean $user_functions if true you need to have defined a function imagecreatefrombmp you can find one http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214
* @return resource/false false if it fails
*/
function imagecreatefromfile($path, &$fun, $user_functions = false)
{
$info = @getimagesize($path);
if(!$info)
{
return false;
}
$functions = array(
IMAGETYPE_GIF => 'imagecreatefromgif',
IMAGETYPE_JPEG => 'imagecreatefromjpeg',
IMAGETYPE_PNG => 'imagecreatefrompng',
IMAGETYPE_WBMP => 'imagecreatefromwbmp',
IMAGETYPE_XBM => 'imagecreatefromwxbm',
);
if($user_functions)
{
$functions[IMAGETYPE_BMP] = 'imagecreatefrombmp';
}
if(!$functions[$info[2]])
{
return false;
}
if(!function_exists($functions[$info[2]]))
{
return false;
}
$fun = str_replace("createfrom", "", $functions[$info[2]]);
$targetImage = $functions[$info[2]]($path);
// fix for png transparency
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
return $targetImage;
}
}