-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.php
executable file
·98 lines (67 loc) · 2.45 KB
/
upload.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
<?php
// chmod 777 /Applications/XAMPP/xamppfiles/htdocs/tut
// chmod 777 /Applications/MAMP/htdocs/tut
header('Content-Type: application/json');
try {
// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if (!isset($_FILES['upfile']['error']) || is_array($_FILES['upfile']['error'])) {
// throw new RuntimeException('Invalid parameters.');
echo '{"message":"Invalid parameters."}';
die();
}
// Check $_FILES['upfile']['error'] value.
switch ($_FILES['upfile']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
echo '{"message":"No file sent."}';
die();
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo '{"message":"Exceeded filesize limit."}';
die();
default:
echo '{"message":"Unknown errors."}';
die();
}
// You should also check filesize here.
if ($_FILES['upfile']['size'] > 1000000) { // 1000000 byte = 1 MB
echo '{"message":"Exceeded filesize limit. Max 1 MB"}';
die();
}
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
echo '{"message":"Invalid file format."}';
die();
}
// You should name it uniquely.
// DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
// On this example, obtain safe unique name from its binary data.
$fileReName = sprintf('%s.%s', sha1_file($_FILES['upfile']['tmp_name']), $ext);
if (!move_uploaded_file($_FILES['upfile']['tmp_name'], './img/' . $fileReName)) {
// If failed to move, check upload directory name and it's permission
echo '{"message":"Failed to move uploaded file."}';
die();
}
// get Extra param with file if needed
$title = $_POST['title'];
// saved path
$savedPath = '/img/'.$fileReName;
// TODO save to database
// $title and $savedPath
echo '{"message":"File is uploaded successfully.", "path":"'.$savedPath.'", "title":"' . $title . '"}';
} catch (RuntimeException $e) {
echo $e->getMessage();
}
?>