-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiletest.php
More file actions
115 lines (102 loc) · 3.83 KB
/
Copy pathfiletest.php
File metadata and controls
115 lines (102 loc) · 3.83 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
107
108
109
110
111
112
113
114
115
<?php
ini_set('display_errors', 'On');
session_start();
//Connects to the database
$mysqli = new mysqli("oniddb.cws.oregonstate.edu","lambja-db","5tGW34Y2vYr1Gy5T","lambja-db");
if(!$mysqli || $mysqli->connect_errno){
$response_array['status'] = "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
//Gather file data
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_FILES["file1"])){
$errorinfo = $_FILES["file1"]["error"];
$filename = $_FILES["file1"]["name"];
$tmpfile = $_FILES["file1"]["tmp_name"];
$filesize = $_FILES["file1"]["size"];
$filetype = $_FILES["file1"]["type"];
//Add photo
//Check that a file was chosen
if (!$tmpfile) { //if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
//Check that file is an image and the correct size
if ($filetype == "image/jpeg" && $filesize < 4294967295) {
//Check that photo name is unique
if(!($stmt = $mysqli->prepare("SELECT count(photo_id) FROM photo WHERE name = ? "))){
echo "Prepare failed: " . $mysqli->errno . " " . $mysqli->error;
}
if(!($stmt->bind_param("s",$filename))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->bind_result($count)){ //Count number of people in class
echo "Bind failed: " . $stmt->errno . " " . $stmt->errno;
}
$stmt->fetch();
$stmt->close();
if($count > 0){
echo "ERROR: Please choose a different file, another photo with this name already exists in the database";
exit();
}
//Insert image into photo
if(!($stmt = $mysqli->prepare("INSERT INTO photo(image) VALUES (?)"))){
echo "Prepare failed: " . $mysqli->errno . " " . $mysqli->error;
}
$null = NULL;
if(!($stmt->bind_param("b",$null))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->send_long_data(0, file_get_contents($tmpfile)))){
echo "Send Long Data failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
}
$photo_id = $stmt->insert_id;
$stmt->close();
//Insert name into photo
if(!($stmt = $mysqli->prepare("UPDATE photo SET name = ? WHERE photo_id = ?"))){
echo "Prepare failed: " . $mysqli->errno . " " . $mysqli->error;
}
if(!($stmt->bind_param("si",$filename,$photo_id))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Added " . $stmt->affected_rows . " rows to photo.";
}
$stmt->close();
//Insert into account_photo
//Get account_id
if(!($stmt = $mysqli->prepare("SELECT account_id FROM account WHERE username = ? "))){
echo "Prepare failed: " . $mysqli->errno . " " . $mysqli->error;
}
if(!($stmt->bind_param("s",$_SESSION['valid_user']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->bind_result($account_id)){ //Account Id of logged in user
echo "Bind failed: " . $stmt->errno . " " . $stmt->errno;
}
$stmt->fetch();
$stmt->close();
if(!($stmt = $mysqli->prepare("INSERT INTO account_photo(account_id, photo_id) VALUES (?,?)"))){
echo "Prepare failed: " . $mysqli->errno . " " . $mysqli->error;
}
if(!($stmt->bind_param("ii",$account_id,$photo_id))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
}
$stmt->close();
} else {
echo "Only jpegs under 4MB are invited to this party.";
}
}
?>