-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalleryupload.php
More file actions
83 lines (70 loc) · 2.65 KB
/
Copy pathgalleryupload.php
File metadata and controls
83 lines (70 loc) · 2.65 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
<?php
session_start();
if(!isset($_SESSION['email']) || empty($_SESSION['email']) ||
!isset($_FILES['gallerypicture']) || empty($_FILES['gallerypicture'])){
http_response_code(401);
echo("Unauthorized");
exit(0);
}
$target_dir = __DIR__ . "/gallery/";
$target_file = $target_dir . basename($_FILES["gallerypicture"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["gallerypicture"]["tmp_name"]);
if(!($check !== false)) {
http_response_code(400);
echo("File is not an image");
exit(0);
}
if (file_exists($target_file)) {
http_response_code(400);
echo("File already exists");
exit(0);
}
if($imageFileType != "jpg" && $imageFileType != "png" &&
$imageFileType != "jpeg" && $imageFileType != "gif" ) {
http_response_code(400);
echo("Incorrect file type");
exit(0);
}
if (move_uploaded_file($_FILES["gallerypicture"]["tmp_name"], $target_file)) {
$database_key = file_get_contents('/api-keys/database.key');
$mysqli_con = new mysqli("localhost","http",$database_key,"cleanlineslawncare");
$valid_database = false;
if(!mysqli_connect_errno()){
$valid_database = true;
$gallery_picture_order = 1;
$sql = "UPDATE gallery_pictures SET gallery_picture_order=gallery_picture_order+1;";
if($stmt = $mysqli_con->prepare($sql)){
$stmt->execute();
$stmt->store_result();
$stmt->close();
} else {
$valid_database = false;
}
$sql = "INSERT IGNORE INTO gallery_pictures (gallery_picture_location, gallery_picture_order, gallery_picture_date) VALUES ( ? , ? ,'" . date("Y-m-d H:i:s") . "');";
if($stmt = $mysqli_con->prepare($sql)){
$gallery_picture_location = basename($_FILES["gallerypicture"]["name"]);
$stmt->bind_param('si', $gallery_picture_location, $gallery_picture_order);
$stmt->execute();
$stmt->store_result();
$stmt->close();
} else {
$valid_database = false;
}
}
$mysqli_con->close();
if( !$valid_database ){
unlink($target_file);
http_response_code(500);
echo("Invalid database");
exit(0);
}
http_response_code(200);
echo("Success!");
exit(0);
} else {
http_response_code(400);
echo("Image couldn't be uploaded");
exit(0);
}
?>