This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.php
122 lines (93 loc) · 4.3 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
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
<?php
/*
* Import the config and uploads json files.
* Make sure your uploads json file has 777 permissions
* otherwise logging uploads will not work.
*/
$config = json_decode(file_get_contents("protected/config.json"), true);
$uploads = json_decode(file_get_contents("protected/uploads.json"), true);
/*
* Getting the values from the config into php variables.
*/
$cfgPassword = $config["password"];
$cfgDirectory = $config["directory"];
$cfgDomain = $config["domain"];
/*
* Defining upload times to be later added to the upload log.
* These values can be later used to format your
* upload date time in the index page.
*
* $upTime is the hour, second, millisecond the file was uploaded at.
*
* $upTimeDayNum is the day number. example: 1.
* $upTimeDayName is the short day name. example: Mon.
* $upTimeDayNameFull is the full day name. example: Monday.
*
* $upTimeMonthNum is the month number. example: 1.
* $upTimeMonthName is the short month name. example: Jan.
* $upTimeMonthNameFull is the full month name. example: January.
*
* $upTimeYear is the full year number. example: 1999.
* $upTimeYearShort is teh short year number. example: 99.
*/
$date = new DateTime();
$upTime = $date->format("h:i:s A");
$upTimeDayNum = $date->format("d");
$upTimeDayName = $date->format("D");
$upTimeDayNameFull = $date->format("l");
$upTimeMonthNum = $date->format("n");
$upTimeMonthName = $date->format("M");
$upTimeMonthNameFull = $date->format("F");
$upTimeYear = $date->format("Y");
$upTimeYearShort = $date->format("y");
/*
* Defining default variables used later.
*
* $hash is used for the new filename.
* $password is the posted password.
* $protocol is checking if website has ssl.
*/
$password = $_POST["password"];
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off" ? "https" : "http" . "://";
$hash = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 8);
/*
* Checking if the posted password is identical to the config password.
* If its not identical it will die with an error code.
*/
if (!isset($password) || $password !== $cfgPassword) {
die("Error 401, bad password.");
}
/*
* The actual upload.
*
* First it checks if the file is empty, if it is
* it will continue with the upload.
*
* After checking the file it will get the file
* extension from the name and define the new location and url.
*
* Finally it will upload the file to the new path (uploads/),
* it will also log the file by added a new array to the upload logs.
*/
if (!empty($_FILES["file"])) {
$extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
$fileurl = $protocol . $cfgDomain . $cfgDirectory . "?f=$hash.$extension";
$filelocation = __DIR__ . "/uploads/$hash.$extension";
if (move_uploaded_file($_FILES['file']['tmp_name'], $filelocation)) {
$uploads["$hash.$extension"]["time"]["time"] = $upTime;
$uploads["$hash.$extension"]["time"]["dayNum"] = $upTimeDayNum;
$uploads["$hash.$extension"]["time"]["dayName"] = $upTimeDayName;
$uploads["$hash.$extension"]["time"]["dayNameFull"] = $upTimeDayNameFull;
$uploads["$hash.$extension"]["time"]["monthNum"] = $upTimeMonthNum;
$uploads["$hash.$extension"]["time"]["monthName"] = $upTimeMonthName;
$uploads["$hash.$extension"]["time"]["monthNameFull"] = $upTimeMonthNameFull;
$uploads["$hash.$extension"]["time"]["year"] = $upTimeYear;
$uploads["$hash.$extension"]["time"]["yearShort"] = $upTimeYearShort;
$uploads["$hash.$extension"]["path"] = "uploads/$hash.$extension";
file_put_contents('protected/uploads.json', json_encode($uploads, JSON_PRETTY_PRINT));
die($fileurl);
} else {
die("Error 502, failure to upload file.");
}
}
?>