-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
106 lines (66 loc) · 2.93 KB
/
Copy pathupload.php
File metadata and controls
106 lines (66 loc) · 2.93 KB
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
<?php
/*
Script:
File upload script
Function:
The goal of this script is to upload an image to the server host machine and add its metadata to the Image table.
This script also generates the name of the file to be uploaded.
A series of checks takes place before the upload happens:
Check if the user is logged in
Check if the upload button was pressed before (to prevent execution of the script without first being at the index page)
Check the file size, units is bytes, limit is 2MB
Confirm if file is indeed an image - allow only PNG and JPG to be uploaded
Then, add the image metadata to the table after the file has been uploaded.
Refresh the page at the end of the script.
*/
//includes
include "class/Handler.php";
include "class/Image.php";
$headerLocation = "Location: signin.php";
//Check if the user is logged in
if (isset($_COOKIE['secured'])) {
//Check if the upload button was pressed before
if(isset($_POST['upload'])) {
$uploadDirectory = "uploads/";
$fileLocation = $uploadDirectory . time() . basename($_FILES['filez']['name']);
$uploadOk = 1;
$fileType = strtolower(pathinfo($fileLocation, PATHINFO_EXTENSION));
//Check the file size, units is bytes, limit is 2MB
if ($_FILES['filez']['size'] > 2000000) {
echo "Your file is larger than 500000! Try again.";
$uploadOk = 0;
}
//Confirm if file is indeed an image
$check = getimagesize($_FILES['filez']['tmp_name']);
if ($check) {
echo "File is an image - " . $check['mime'] . ".";
} else {
echo "File is not an image. Upload failed.";
$uploadOk = 0;
}
//Allow only PNG and JPG to be uploaded
if (!($fileType == "png" || $fileType == "jpg" || $fileType == "jpeg")) {
echo "Only .png and .jpg files are allowed!";
$uploadOk = 0;
}
//Complete the upload procedure
if ($uploadOk === 1) {
$uploadStatus = move_uploaded_file($_FILES['filez']['tmp_name'], $fileLocation);
if ($uploadStatus) {
echo "The file " . basename($_FILES['filez']['name']) . " has been uploaded successfully!";
//Make a DB entry for the image if upload is successful
//DB connection using Handler class
$handler = new Handler("root", "", "UploadImages");
$handler->connect();
$errorCode = Image::create($handler, $_COOKIE['connected'], $_POST['title'], $fileLocation);
$handler = null;
} else {
echo "Error uploading file.";
}
} else {
echo "Error uploading file (end of code).";
}
}
}
header($headerLocation);
exit();