76 lines
3.1 KiB
PHP
76 lines
3.1 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.php';
|
|
require '../../../assets/utils/classes/FileUploader.php';
|
|
|
|
try {
|
|
|
|
// ── Fetch current picture filename from DB ────────────────
|
|
$sth = $pdo1->prepare(
|
|
'SELECT profile_picture FROM user WHERE user_id = :user_id LIMIT 1'
|
|
);
|
|
$sth->execute([':user_id' => $user_id]);
|
|
$current_picture = $sth->fetchColumn() ?: '';
|
|
|
|
// ── Handle new picture upload ─────────────────────────────
|
|
$db_picture = $current_picture;
|
|
|
|
if (!empty($_FILES['profile_picture']['name'])) {
|
|
|
|
// Remove old file if exists
|
|
if (!empty($current_picture)) {
|
|
$old_path = $include_url . 'uploads/profile/' . $current_picture;
|
|
if (file_exists($old_path)) unlink($old_path);
|
|
}
|
|
|
|
$uploader = new FileUploader($include_url . 'uploads/profile/');
|
|
$errors = $uploader->upload('profile_picture');
|
|
|
|
if (!empty($errors)) {
|
|
$answer['message'] = $errors[0];
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
$db_picture = $uploader->getUploadedFiles()[0] ?? '';
|
|
}
|
|
|
|
// ── Update user record ────────────────────────────────────
|
|
$sth = $pdo1->prepare(
|
|
"UPDATE user SET
|
|
name = :name,
|
|
surname = :surname,
|
|
email = :email,
|
|
phone = :phone,
|
|
country = :country,
|
|
address = :address,
|
|
profile_picture = :profile_picture
|
|
WHERE user_id = :user_id"
|
|
);
|
|
$sth->execute([
|
|
':name' => trim($data['name'] ?? ''),
|
|
':surname' => trim($data['surname'] ?? ''),
|
|
':email' => trim($data['email'] ?? ''),
|
|
':phone' => trim($data['phone'] ?? ''),
|
|
':country' => trim($data['country'] ?? ''),
|
|
':address' => trim($data['address'] ?? ''),
|
|
':profile_picture' => $db_picture,
|
|
':user_id' => $user_id,
|
|
]);
|
|
db_check($sth, $answer);
|
|
|
|
// ── Refresh session ───────────────────────────────────────
|
|
$_SESSION['login_name'] = trim($data['name'] ?? '');
|
|
$_SESSION['login_surname'] = trim($data['surname'] ?? '');
|
|
$_SESSION['login_profile_picture'] = $db_picture;
|
|
|
|
$answer['success'] = 1;
|
|
$answer['message'] = 'Profile updated.';
|
|
$answer['profile_picture'] = $db_picture;
|
|
|
|
} catch (Exception $e) {
|
|
$answer['message'] = 'Failed to update profile.';
|
|
http_response_code(500);
|
|
}
|
|
|
|
exit(json_encode($answer)); |