-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-profile.php
More file actions
121 lines (109 loc) · 4.23 KB
/
Copy pathedit-profile.php
File metadata and controls
121 lines (109 loc) · 4.23 KB
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
<?php
session_start();
require_once 'config.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE user_id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();
if (!$user) {
header("Location: login.php");
exit();
}
$error = "";
$success = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
// Handle file upload
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["image"]["name"];
$filetype = $_FILES["image"]["type"];
$filesize = $_FILES["image"]["size"];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!array_key_exists($ext, $allowed)) {
$error = "Please select a valid file format.";
} else {
$maxsize = 5 * 1024 * 1024; // 5MB
if ($filesize > $maxsize) {
$error = "File size is larger than the allowed limit.";
} else {
// Create uploads directory if it doesn't exist
if (!file_exists('uploads')) {
mkdir('uploads', 0777, true);
}
// Generate a unique filename
$new_filename = "uploads/" . uniqid() . "." . $ext;
if (move_uploaded_file($_FILES["image"]["tmp_name"], $new_filename)) {
$image = $new_filename;
} else {
$error = "Failed to upload image. Error: " . error_get_last()['message'];
}
}
}
} else {
$image = $user['image'];
}
if (empty($error)) {
$stmt = $pdo->prepare("UPDATE users SET first_name = ?, last_name = ?, email = ?, image = ? WHERE user_id = ?");
try {
$stmt->execute([$first_name, $last_name, $email, $image, $user_id]);
$success = "Profile updated successfully!";
} catch (PDOException $e) {
$error = "Error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Profile - Realtime Chat App</title>
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
</head>
<body>
<div class="wrapper">
<section class="form edit-profile">
<header>Edit Profile</header>
<form action="" method="POST" enctype="multipart/form-data">
<?php if (!empty($error)): ?>
<div class="error-text" style=" color: #721c24; background: #f8d7da; padding: 8px 10px; text-align: center; border-radius: 5px; margin-bottom: 10px; border: 1px solid #f5c6cb;"><?php echo $error; ?></div>
<?php endif; ?>
<?php if (!empty($success)): ?>
<div class="success-text" style=" color: #00c476; background: hsl(156, 100%, 38%, 15%); padding: 8px 10px; text-align: center; border-radius: 5px; margin-bottom: 10px; border: 1px solid #93f3cc;"><?php echo $success; ?></div>
<?php endif; ?>
<div class="field input">
<label>First Name</label>
<input type="text" name="first_name" value="<?php echo $user['first_name']; ?>" required>
</div>
<div class="field input">
<label>Last Name</label>
<input type="text" name="last_name" value="<?php echo $user['last_name']; ?>" required>
</div>
<div class="field input">
<label>Email Address</label>
<input type="email" name="email" value="<?php echo $user['email']; ?>" required>
</div>
<div class="field image">
<label>Profile Image</label>
<input type="file" name="image" accept="image/x-png,image/gif,image/jpeg,image/jpg">
</div>
<div class="field button">
<input type="submit" value="Update Profile">
</div>
</form>
<div class="link">
<a href="profile.php">Back to Profile</a>
</div>
</section>
</div>
</body>
</html>