%PDF- %GIF98; %PNG;
Server : ApacheSystem : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64 User : ndkhermy ( 1200) PHP Version : 8.5.12 Disable Function : NONE Directory : /home1/ndkhermy/www/website_6ca89795/api/ |
Upload File : |
<?php
header("Content-Type: application/json");
include("../dbcon.php");
include("auth.php");
function response($status,$message,$data=[])
{
echo json_encode([
"status"=>$status,
"message"=>$message,
"data"=>$data
],JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
exit;
}
/*
|--------------------------------------------------------------------------
| Support Form Data + Raw JSON
|--------------------------------------------------------------------------
*/
$json = json_decode(file_get_contents("php://input"),true);
$username = trim(
$_REQUEST['username']
?? $json['username']
?? ''
);
$email = trim(
$_REQUEST['email']
?? $json['email']
?? ''
);
$phone = trim(
$_REQUEST['phone']
?? $json['phone']
?? ''
);
/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/
if(empty($username) || empty($email))
{
response(false,"Required Fields Missing",[
"required_fields" => [
"username",
"email"
],
"example_url" =>
BASE_URL .
"api/profile_update.php?username=Siddharth%20Jha&email=test@gmail.com&phone=8210302952",
"sample_request" => [
"username" => "Siddharth Jha",
"email" => "test@gmail.com",
"phone" => "8210302952"
],
"postman_form_data" => [
[
"key" => "username",
"value" => "Siddharth Jha"
],
[
"key" => "email",
"value" => "test@gmail.com"
],
[
"key" => "phone",
"value" => "8210302952"
],
[
"key" => "profile_image",
"value" => "(file)"
]
],
"raw_json_example" => [
"username" => "Siddharth Jha",
"email" => "test@gmail.com",
"phone" => "8210302952"
],
"received_data" => [
"request" => $_REQUEST,
"json" => $json
]
]);
}
/*
|--------------------------------------------------------------------------
| Email Exists Check
|--------------------------------------------------------------------------
*/
$checkEmail = mysqli_query(
$conn,
"SELECT id
FROM user
WHERE email='".mysqli_real_escape_string($conn,$email)."'
AND id!='".$user_id."'"
);
if(mysqli_num_rows($checkEmail) > 0)
{
response(false,"Email Already Exists");
}
/*
|--------------------------------------------------------------------------
| Get Existing User
|--------------------------------------------------------------------------
*/
$userQry = mysqli_query(
$conn,
"SELECT *
FROM user
WHERE id='$user_id'
LIMIT 1"
);
$user = mysqli_fetch_assoc($userQry);
$profile_image = $user['profile_image'];
/*
|--------------------------------------------------------------------------
| Upload Profile Image
|--------------------------------------------------------------------------
*/
if(
isset($_FILES['profile_image']) &&
$_FILES['profile_image']['error'] == 0
)
{
$allowed = ['jpg','jpeg','png','webp'];
$ext = strtolower(
pathinfo(
$_FILES['profile_image']['name'],
PATHINFO_EXTENSION
)
);
if(!in_array($ext,$allowed))
{
response(false,"Only JPG, JPEG, PNG, WEBP Allowed");
}
$profile_image =
"profile_" .
time() .
rand(1000,9999) .
"." .
$ext;
move_uploaded_file(
$_FILES['profile_image']['tmp_name'],
"../admin/dataimg/" . $profile_image
);
}
/*
|--------------------------------------------------------------------------
| Update Profile
|--------------------------------------------------------------------------
*/
mysqli_query(
$conn,
"UPDATE user SET
username='".mysqli_real_escape_string($conn,$username)."',
email='".mysqli_real_escape_string($conn,$email)."',
phone='".mysqli_real_escape_string($conn,$phone)."',
profile_image='".mysqli_real_escape_string($conn,$profile_image)."'
WHERE id='$user_id'"
);
/*
|--------------------------------------------------------------------------
| Fetch Updated User
|--------------------------------------------------------------------------
*/
$userQry = mysqli_query(
$conn,
"SELECT *
FROM user
WHERE id='$user_id'
LIMIT 1"
);
$user = mysqli_fetch_assoc($userQry);
/*
|--------------------------------------------------------------------------
| Response
|--------------------------------------------------------------------------
*/
response(true,"Profile Updated Successfully",[
"user_id" =>
$user['id'],
"username" =>
$user['username'],
"email" =>
$user['email'],
"phone" =>
$user['phone'],
"profile_image" =>
!empty($user['profile_image'])
? BASE_URL .
"admin/dataimg/" .
$user['profile_image']
: ""
]);
$conn->close();
?>