prepare("SELECT name, phone, balance, profile_pic, level FROM users WHERE phone = ?"); $stmt->execute([$_SESSION['phone']]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if (!$user) { header('Location: login.php'); // Redirect if the user is not found exit(); } // Handle profile picture upload $error_message = ''; $success_message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_FILES['profile_pic']) && $_FILES['profile_pic']['error'] === UPLOAD_ERR_OK) { $file_tmp_path = $_FILES['profile_pic']['tmp_name']; // Generate signature for Cloudinary $timestamp = time(); $signature_string = "timestamp=$timestamp$api_secret"; $signature = sha1($signature_string); $upload_url = "https://api.cloudinary.com/v1_1/$cloud_name/image/upload"; $data = [ "file" => new CURLFile($file_tmp_path), "api_key" => $api_key, "timestamp" => $timestamp, "signature" => $signature, ]; // Initialize cURL for Cloudinary upload $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $upload_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); curl_close($ch); // Process response from Cloudinary if ($response !== false) { $response_data = json_decode($response, true); if (isset($response_data['secure_url'])) { $profile_pic_url = $response_data['secure_url']; // Update the user's profile picture in the database $update_stmt = $pdo->prepare("UPDATE users SET profile_pic = ? WHERE phone = ?"); if ($update_stmt->execute([$profile_pic_url, $_SESSION['phone']])) { $success_message = "Profile picture updated successfully."; $user['profile_pic'] = $profile_pic_url; // Update the profile pic locally } else { $error_message = "Failed to update profile picture in the database."; } } else { $error_message = "Cloudinary upload failed: " . $response_data['error']['message']; } } else { $error_message = "Failed to upload image to Cloudinary."; } } else { $error_message = "Please select a valid profile picture to upload."; } } ?>
= htmlspecialchars($error_message) ?>
= htmlspecialchars($success_message) ?>