-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
52 lines (46 loc) · 1.48 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
<?php include("_header.php");?>
<?php
function put_to_server($url){
if ($_SESSION["userID"] === ""){
$user = 0;
}else{
$user = $_SESSION["userID"]; //Set to 0 for now, will eventually take in the userID of the person currently logged in.
}
$mysqli = new mysqli("oniddb.cws.oregonstate.edu","mugicaj-db","A9m4Bu2nBA0JDc1P","mugicaj-db");
if(!($mysqli->query("insert into Links(UID,url) values($user,'$url')"))){
printf("Did not save to database");
}
}
$img=$_FILES['img'];
if(isset($_POST['submit'])){
if($img['name']==''){
echo "<h2>An Image Please.</h2>";
}else{
$filename = $img['tmp_name'];
$client_id="ee8ea002d5494d8";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url=$pms['data']['link'];
if($url!=""){
put_to_server($url);
echo "<h2>Uploaded Without Any Problem</h2>";
}else{
echo "<h2>There's a Problem: Imgur</h2>";
echo $pms['data']['error'];
}
}
}
?>
<?php include("_footer.php");?>