forked from SAAR-IITP/SAAR-IITP
-
Notifications
You must be signed in to change notification settings - Fork 13
/
newPost.php
68 lines (66 loc) · 3.02 KB
/
newPost.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
<?php
session_start();
$error = array();
$photo = array();
if(isset($_POST['submit'])){
// saving the photos
$j = 0; // Variable for indexing uploaded image.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
if($_FILES['file']['name'][$i]){
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
$target_path = "./uploads/"; // Declaring Path for uploaded images.
$file_extension = strtolower(end($ext)); // Store extensions in the variable.
$unique = md5(uniqid()) . "." . $ext[count($ext) - 1];
$target_path = $target_path . $unique; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 1000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
// If file moved to uploads folder.
$photo[] = $unique;
} else { // If File Was Not Moved.
$error[] = $j. ').Please try again!.<br/>';
}
} else { // If File Size And File Type Was Incorrect.
$error[] = $j. ').Invalid file Size or Type. <br/>'.$_FILES['file']['name'][0];
}
}
}
if(!empty($error)){
$_SESSION['msg'] = $error[0];
header("location:chat.php");
}else{
// making api call
$images = serialize($photo);
$url = 'http://api.saar.iitp.ac.in/createPost.php';
$ch = curl_init($url);
$data = array(
'user_id' => $_POST['user_id'],
'user_name' => $_POST['user_name'],
'user_img' => $_POST['user_img'],
'cat_id' => $_POST['cat_id'],
'title' => $_POST['title'],
'body' => $_POST['body'],
'images' => $images
);
$payload = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, true);
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
//close cURL resource
curl_close($ch);
$response = json_decode($result,true);
// echo $response['messages'][0];
if($response['status'] == 209 || $response['status'] == 408){
$_SESSION['msg'] = $response['messages'][0];
header("location:chat.php");
}
}
}
?>