-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-uploader.php
223 lines (165 loc) · 7.45 KB
/
image-uploader.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
<?php
/**
* CJ IMAGE UPLOADER
* -POST params-
* @param $firstName - first name of submitter
* @param $lastName - last name of submitter
* @param $email - email address of submitter
* @param $category - category of the submission
* @param $caption - caption of the submission
* @param $image - image file that is being uploaded
* -optional-
* @param $ajaxEnabled - if isset, form is being submitted via ajax, so return a json response
*/
ini_set('memory_limit', '128M');
header('Content-Type: application/json');
set_error_handler('customError');
set_exception_handler('customException');
define("DEBUG", true);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
include('credentials.php');
$output = array("ok" => false);
$timestamp = time();
$firstName = substr($_POST['firstName'],0,64);
$lastName = substr($_POST['lastName'],0,64);
$email = substr($_POST['email'],0,64);
$category = substr($_POST['category'],0,128);
$caption1 = substr($_POST['caption1'],0,1024);
$image1 = $_FILES['image1'];
isset($_FILES['image2']);
isset($_FILES['image3']);
isset($_POST['caption2']);
isset($_POST['caption3']);
if ($image1['error'] != 0){
output($image1['error'], false);
} else {
/***Get image info**/
$cleanName1 = trim($image1['name']);
if (strlen($cleanName1) > 10){
$cleanName1 = substr($cleanName1, 0, 9);
}
// $string1 = filter_var($cleanName1, FILTER_SANITIZE_STRING);
$imageInfo1 = pathinfo($image1['name']);
$filename1 = $timestamp . "." . strtolower($cleanName1) . "." . strtolower($imageInfo1['extension']);
// $filename1 = $timestamp . "." . strtolower($cleanName1) . "." . strtolower($imageInfo1['extension']);
/***Save entry to the database***/
//Connect to mysql DB
// $db = mysql_connect($dbhost, $dbuser, $dbpass);
$db1 = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//Insert new record
$stmt1 = $db1->prepare("INSERT INTO {$dbtable}(first_name,last_name,email,caption,category,file_name) VALUES(:first_name,:last_name,:email,:caption,:category,:file_name)");
$stmt1->execute(array(':first_name' => $_POST['firstName'], ':last_name' => $_POST['lastName'], ':email' => $_POST['email'], ':caption' => $_POST['caption1'], ':category' => $_POST['category'], ':file_name' => $filename1));
// $affected_rows = $stmt1->rowCount();
// if ($affected_rows == 1){
//Move the uploaded file into place
move_uploaded_file($image1['tmp_name'], 'images/'. $filename1);
//Create a thumbnail
include('smart_resize.php');
smart_resize_image('images/'. $filename1, null, 150 , 150 , true , 'images/th_' . $filename1, false, false, 80);
// output("Success! We received your submission.", true);
// }
// else{
// output("Could not save information", false);
// }
};
if (!empty($_FILES['image2'])) {
$image2 = $_FILES['image2'];
$caption2 = substr($_POST['caption2'],0,1024);
if ($image2['error'] != 0){
output($image2['error'], false);
} else {
/***Get image info**/
$cleanName2 = trim($image2['name']);
if (strlen($cleanName2) > 10){
$cleanName2 = substr($cleanName2, 0, 9);
}
// $string2 = filter_var($cleanName2, FILTER_SANITIZE_STRING);
$imageInfo2 = pathinfo($image2['name']);
$filename2 = $timestamp . "." . strtolower($cleanName2) . "." . strtolower($imageInfo2['extension']);
// $filename2 = $timestamp . "." . strtolower($cleanName2) . "." . strtolower($imageInfo2['extension']);
/***Save entry to the database***/
//Connect to mysql DB
// $db = mysql_connect($dbhost, $dbuser, $dbpass);
$db2 = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//Insert new record
$stmt2 = $db2->prepare("INSERT INTO {$dbtable}(first_name,last_name,email,caption,category,file_name) VALUES(:first_name,:last_name,:email,:caption,:category,:file_name)");
$stmt2->execute(array(':first_name' => $_POST['firstName'], ':last_name' => $_POST['lastName'], ':email' => $_POST['email'], ':caption' => $_POST['caption2'], ':category' => $_POST['category'], ':file_name' => $filename2));
$affected_rows2 = $stmt2->rowCount();
// if ($affected_rows2 == 1){
// Move the uploaded file into place
move_uploaded_file($image2['tmp_name'], 'images/'. $filename2);
//Create a thumbnail
// include('smart_resize.php');
smart_resize_image('images/'. $filename2, null, 150 , 150 , true , 'images/th_' . $filename2, false, false, 80);
// output("Success! We received your submission.", true);
// }
// else{
// output("Could not save information", false);
// }
};
}
if (!empty($_FILES['image3'])) {
$image3 = $_FILES['image3'];
$caption3 = substr($_POST['caption3'],0,1024);
if ($image3['error'] != 0){
output($image3['error'], false);
} else {
/***Get image info**/
$cleanName3 = trim($image3['name']);
if (strlen($cleanName3) > 10){
$cleanName3 = substr($cleanName3, 0, 9);
}
// $string3 = filter_var($cleanName3, FILTER_SANITIZE_STRING);
$imageInfo3 = pathinfo($image3['name']);
$filename3 = $timestamp . "." . strtolower($cleanName3) . "." . strtolower($imageInfo3['extension']);
// $filename3 = $timestamp . "." . strtolower($cleanName3) . "." . strtolower($imageInfo3['extension']);
/***Save entry to the database***/
//Connect to mysql DB
// $db = mysql_connect($dbhost, $dbuser, $dbpass);
$db3 = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//Insert new record
$stmt3 = $db3->prepare("INSERT INTO {$dbtable}(first_name,last_name,email,caption,category,file_name) VALUES(:first_name,:last_name,:email,:caption,:category,:file_name)");
$stmt3->execute(array(':first_name' => $_POST['firstName'], ':last_name' => $_POST['lastName'], ':email' => $_POST['email'], ':caption' => $_POST['caption3'], ':category' => $_POST['category'], ':file_name' => $filename3));
$affected_rows3 = $stmt3->rowCount();
// if ($affected_rows3 == 1){
//Move the uploaded file into place
move_uploaded_file($image3['tmp_name'], 'images/'. $filename3);
//Create a thumbnail
// include('smart_resize.php');
smart_resize_image('images/'. $filename3, null, 150 , 150 , true , 'images/th_' . $filename3, false, false, 80);
// output("Success! We received your submission.", true);
// }
// else{
// output("Could not save information", false);
// }
};
}
output("Success! We received your submission.", true);
function output($message, $isError){
if (isset($_POST['ajaxEnabled'])){
echo json_encode(array("ok" => $isError, "message" => $message));
}
else{
echo $message;
}
}
//Error and exception handling
function customError($errno, $errmsg){
if (DEBUG){
die( '{"ok": false, "message" : "' . $errmsg . '"}');
echo $errmsg;
}else{
die( '{"ok": false, "message" : "error #' . $errno . '"}');
echo $errno;
}
}
function customException($err){
if (DEBUG){
die( '{"ok": false, "message" : "'. $err .'"}');
echo $err;
}else{
die( '{"ok": false, "message" : "unhandled exception"}');
echo "unhandled exception";
}
}
?>