-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsample-php-function-for-upload.php
124 lines (99 loc) · 3.7 KB
/
sample-php-function-for-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
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
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
$json_array = new stdClass();
if (isset($postdata)) {
$request = json_decode(trim($postdata));
if($request === null) {
//error validation
$json_array->error->code = 1;
$json_array->error->message = 'Invalid JSON Data';
echo json_encode($json_array);
exit;
}
if($request->api_key != 'XYZ2017'){
header('HTTP/1.1 403 Unauthorized', true, 403);
$json_array->error->code = 1;
$json_array->error->message = 'Invalid API Key';
echo json_encode($json_array);
exit;
}
}else{
//error validation
$json_array->error->code = 1;
$json_array->error->message = 'Please fill all required fields';
echo json_encode($json_array);
exit;
}
function updateProfile($postdata){
$json_array = new stdClass();
$result = updateProfileDetails($postdata);
if($result){
$json_array->message = 'Successfully saved';
$json_array->status = 'Success';
}else{
$json_array->status = 'Error';
$json_array->message = 'Unable to save';
}
echo json_encode($json_array);
exit;
}
?>
/** IONIC Request
$scope.updateProfile = function(postdata) {
jQuery('.account_form .required').each(function(index){
if(jQuery(this).val() == ''){
jQuery(this).parent().addClass('error');
}else{
jQuery(this).parent().removeClass('error');
}
});
if(jQuery('.account_form .required').parent().hasClass('error')) return false;
if ($scope.profile_image != null && $scope.profile_image.length > 0) {
var savedImages = $scope.profile_image;
}else{
var savedImages = '';
}
var current_user = AuthService.current_user();
$scope.ajaxDisplayProject = true;
$http.post(WEB_SERVICES.api_url, {api_key:WEB_SERVICES.api_key, action : 'update-profile',name:postdata.name,companyname:postdata.companyname,city:postdata.city,stategroup:postdata.stategroup,zipcode:postdata.zipcode,address:postdata.address,phonenumber:postdata.phonenumber,fax:postdata.fax,website:postdata.website,profile_image:savedImages,user_id:current_user.id}).then(function (response){
var alertPopup = $ionicPopup.alert({
title: response.data.status,
template: response.data.message
});
$scope.ajaxDisplayProject = false;
if(response.data.status =='Success'){
$state.go('main.settings', {}, {reload: true});
}
});
}
*/
function updateProfileDetails($postdata){
$profile_image = $postdata->profile_image;
$upload_path =JPATH_ROOT.'/images/avatars/';
if(!empty($profile_image)){
$avatar_image ='';
$img_base64 = str_replace('data:image/png;base64,', '', $profile_image);
$img_base64 = str_replace('data:image/jpg;base64,', '', $img_base64);
$img_base64 = str_replace('data:image/jpeg;base64,', '', $img_base64);
$img_base64 = str_replace('data:image/gif;base64,', '', $img_base64);
$img_base64 = str_replace(' ', '+', $img_base64);
$decoded=base64_decode($img_base64);
$mimetype = getImageMimeType($decoded);
$image_name = uniqid() . '.'.$mimetype;
file_put_contents($upload_path.$image_name,$decoded);
$avatar_image = $image_name;
}
}