-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
279 lines (248 loc) · 8.1 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
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
<?php
################
# UPLOAD CLASS #
################
class upload
{
//creates global connection variable for the class
protected static $connection;
//creates a variable that holds styling for error messages
public static $style = "<p class='error'>";
//creates an array that will hold messages for different errors
public static $messages = array();
//variable that holds the maximum number images allowed to upload
public $num_files = null;
//creates a variable that will hold the temporary image
public $file = null;
//creates the temporary desination directory for the image
public static $temp = "images/temp/";
//creates the desination directory for the image
public static $destination = "images/";
public static $i = 0;
//constructor for the class
public function __construct($in_connection, $in_file, $in_num) {
$this->file = $in_file;
$this->num_files = $in_num;
upload::get_connection($in_connection);
if (is_dir(upload::$temp) && is_dir(upload::$destination)
&& is_writable(upload::$temp) && is_writable(upload::$destination)) {
$query = "SELECT * FROM temp_images";
$result = mysqli_query($in_connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$row['image_link'];
upload::$i++;
}
if (upload::$i == 0) {
$this->check_file();
}
else if (upload::$i > 0 && (upload::$i < $this->num_files)) {
$this->check_file();
}
else {
upload::$messages[] = upload::$style . "You have uploaded
the max number of
images alloted";
}
}
else {
upload::$messages[] = upload::$style . "File upload feature
is not available";
}
}
public static function get_connection($in_connection) {
upload::$connection = $in_connection;
}
//checks to see if their was a file chosen
public function check_file() {
$error = $_FILES['image']['error'];
switch ($error) {
case UPLOAD_ERR_NO_FILE:
upload::$messages[] = upload::$style . "Please select a file to upload";
break;
case UPLOAD_ERR_PARTIAL:
upload::$messages[] = upload::$style . "File was unable to upload,
please try again";
break;
case UPLOAD_ERR_INI_SIZE:
upload::$messages[] = upload::$style . "File is too big,
please select a file under
2 Megabytes";
break;
case UPLOAD_ERR_CANT_WRITE:
upload::$messages[] = upload::$style . "File was unable to upload,
please try again";
break;
case UPLOAD_ERR_NO_TMP_DIR:
upload::$messages[] = upload::$style . "File was unable to upload,
please try again";
break;
case UPLOAD_ERR_OK:
$this->check_file_type();
break;
}
}
//checks to see if the file uploaded is of the right type
public function check_file_type() {
//creates a boolean variable
$yes = false;
//creates an array of allowed mime types
$types = array("jpg"=>'image/jpeg',
"png"=>'image/png',
"tiff"=>'image/tiff');
//checks the mime type of the file and saves it into a variable
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $this->file);
//runs a for each loop to show contents of types array
foreach ($types as $value) {
/*if the mime type of the file matches a mime type in the array
set boolean to true*/
if ($mime_type == $value) {
$yes = true;
}
}
finfo_close($finfo);
/*if mime type isn't matched in the foreach loop put
error in messages array*/
if (!$yes) {
upload::$messages[] = upload::$style . "Please select a different filetype,
permitted file types
(jpeg, png, tiff)";
}
//if the mime type matches allowed mime type call trim file function
else {
$this->trim_filename();
}
}
//replaces the filename with an incremented number
public function trim_filename() {
//querys the count table to get the current
$query = "SELECT * FROM image_count WHERE count = 1";
$result = mysqli_query(upload::$connection, $query);
if (!$result) {
die("Database query failed. ");
}
$row = mysqli_fetch_assoc($result);
//adds one to the count
$count = 1 + $row["name"];
//echo $count;
//querys the database to enter the new count into the count table
$query = "UPDATE image_count SET name = $count WHERE count = 1";
$result = mysqli_query(upload::$connection, $query);
if (!$result) {
die("Database query failed. ");
}
//gets file name of the uploaded file
$file = $_FILES['image']['name'];
//seperates the file string to get the file extension
$file = explode(".", $file);
$file = end($file);
//concatinates the count to the file extension
$file = $count . "." . $file;
//saves the new name into the global files array
$_FILES['image']['name'] = $file;
//calls the final check function
$this->final_check();
}
//performs the final check and moves the file/queries the database
public function final_check() {
$verdict = null;
foreach (upload::$messages as $value) {
if (!is_null($value)) {
$verdict = 1;
}
}
if (is_null($verdict)) {
move_uploaded_file($this->file, upload::$temp . $_FILES['image']['name']);
$file_db = $_FILES['image']['name'];
$query = "INSERT INTO temp_images VALUES ('{$file_db}')";
$result = mysqli_query(upload::$connection, $query);
//sets a success message
upload::$messages[] = "<p class='verdict'>"
. "File uploaded successfully";
}
else {
upload::$messages[] = upload::$style . "Something went wrong, please try
again later.";
}
}
//queries the database and stores the image path
public static function database_query($dir, $type, $table, $column,
$column_two, $id) {
if (is_dir(upload::$temp)
&& is_dir(upload::$destination . $dir)
&& is_writable(upload::$destination . $dir)) {
if (!empty($_POST['page'])) {
foreach ($_POST['page'] as $value) {
$rename = rename(upload::$temp . $value, upload::$destination . $dir . $value);
unset ($_POST['page']);
if (is_null($column_two)) {
$query = "$type $table SET $column = '{$value}'";
}
else {
$query = "$type $table ($column,
$column_two) VALUES ('{$value}', $id)";
}
$result = mysqli_query(upload::$connection, $query);
if (!$result) {
die("Database query failed. ");
}
}
upload::$messages[] = "<p class='verdict'>" . "Images
have been successfully saved";
}
else {
upload::$messages[] = upload::$style . "Please upload an image";
}
}
else {
upload::$messages[] = upload::$style . "Images could not be saved,
please try again";
}
}
public static function delete_temp($in_connection, $image) {
if (is_dir(upload::$temp) && is_writable(upload::$temp)) {
$path = upload::$temp . $image;
if (is_file($path)) {
$query = "SELECT image_link FROM temp_images WHERE image_link = '{$image}'";
$result = mysqli_query($in_connection, $query);
$row = mysqli_fetch_assoc($result);
unlink ($path);
unset($row['image_link']);
$query = "DELETE FROM temp_images WHERE image_link = '{$image}' LIMIT 1";
$result = mysqli_query($in_connection, $query);
upload::$messages[] = upload::$style . "Image was deleted";
}
}
else {
upload::$messages[] = upload::$style . "Image could
not be deleted, please try again";
}
}
public static function delete_image($table, $column, $image, $dir) {
if (is_dir(upload::$temp)
&& is_writable(upload::$temp)
&& is_dir(upload::$destination)
&& is_writable(upload::$destination)) {
$path = upload::$destination . $dir . $image;
if (is_file($path)) {
unlink ($path);
$query = "DELETE FROM $table WHERE $column = '{$image}'";
$result = mysqli_query(upload::$connection, $query);
if (!$result) {
die("Database query failed. ");
}
upload::$messages[] = "<p class='verdict'>" . "Image was
deleted successfully";
}
else {
upload::$messages[] = upload::$style . "Image cannot
be deleted, please try again";
}
}
else {
upload::$messages[] = upload::$style . "Image can not
be deleted, please try again";
}
}
}
?>