Implemented user avatar support

This commit is contained in:
root 2019-03-22 21:38:08 -04:00
parent 3f34bac863
commit 8063c627f8
9 changed files with 53 additions and 7 deletions

View File

@ -7,7 +7,7 @@
<span aria-hidden="true">&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="modal-body">
<div class="form-group">
<label>Name</label>
@ -38,6 +38,10 @@
<input type="password" class="form-control" name="password" placeholder="Enter a Password" required>
</div>
</div>
<div class="form-group">
<label>Avatar</label>
<input type="file" class="form-control-file" accept="image/*;capture=camera" name="avatar">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

View File

@ -12,5 +12,6 @@
$sql = mysqli_query($mysqli,"SELECT * FROM users WHERE user_id = $session_user_id");
$row = mysqli_fetch_array($sql);
$session_name = $row['name'];
$session_avatar = $row['avatar'];
?>

View File

@ -7,10 +7,14 @@
<span aria-hidden="true">&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
<input type="hidden" name="current_password_hash" value="<?php echo $password; ?>">
<input type="hidden" name="current_avatar_path" value="<?php echo $avatar; ?>">
<div class="modal-body">
<center class="mb-3">
<img class="img-fluid rounded-circle" src="<?php echo $avatar; ?>" height="128" width="128">
</center>
<div class="form-group">
<label>Name</label>
<div class="input-group">
@ -40,6 +44,10 @@
<input type="password" class="form-control" name="password" placeholder="Enter a password" value="<?php echo $password; ?>" required>
</div>
</div>
<div class="form-group">
<label>Avatar</label>
<input type="file" class="form-control-file" accept="image/*;capture=camera" name="avatar">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

View File

@ -14,6 +14,19 @@ if(isset($_POST['add_user'])){
mysqli_query($mysqli,"INSERT INTO users SET name = '$name', email = '$email', password = '$password'");
$user_id = mysqli_insert_id($mysqli);
$check = getimagesize($_FILES["avatar"]["tmp_name"]);
if($check !== false) {
$avatar_path = "uploads/user_avatars/";
$avatar_path = $avatar_path . $user_id . '_' . time() . '_' . basename( $_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'], $avatar_path);
}else{
$avatar_path = "img/default_user_avatar.png";
}
mysqli_query($mysqli,"UPDATE users SET avatar = '$avatar_path' WHERE user_id = $user_id");
$_SESSION['alert_message'] = "User added";
header("Location: users.php");
@ -32,8 +45,18 @@ if(isset($_POST['edit_user'])){
}else{
$password = md5($password);
}
mysqli_query($mysqli,"UPDATE users SET name = '$name', email = '$email', password = '$password' WHERE user_id = $user_id");
$avatar_path = $_POST['current_avatar_path'];
$check = getimagesize($_FILES["avatar"]["tmp_name"]);
if($check !== false) {
if($avatar_path != "img/default_user_avatar.png"){
unlink($avatar_path);
}
$avatar_path = "uploads/user_avatars/";
$avatar_path = $avatar_path . $user_id . '_' . time() . '_' . basename( $_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'], "$avatar_path");
}
mysqli_query($mysqli,"UPDATE users SET name = '$name', email = '$email', password = '$password', avatar = '$avatar_path' WHERE user_id = $user_id");
$_SESSION['alert_message'] = "User updated";

View File

@ -22,9 +22,15 @@
<ul class="navbar-nav ml-auto ml-md-0">
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-user-circle fa-fw"></i> <?php echo "$session_name"; ?>
<img height="28" width="28" src="<?php echo "$session_avatar"; ?>" class="img-fluid rounded-circle"> <?php echo "$session_name"; ?>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="userDropdown">
<a class="dropdown-item" data-toggle="modal" data-target="#changeAvatarModal" href="#">
<center>
<img height="128" width="128" src="<?php echo "$session_avatar"; ?>" class="img-fluid rounded-circle">
</center>
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Settings</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="logout.php">Logout</a>

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -13,7 +13,7 @@
<table class="table table-striped table-borderless table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th class="text-center">Name</th>
<th>Email</th>
<th class="text-center">Actions</th>
</tr>
@ -26,10 +26,14 @@
$name = $row['name'];
$email = $row['email'];
$password = $row['password'];
$avatar = $row['avatar'];
?>
<tr>
<td><?php echo $name; ?></a></td>
<td class="text-center">
<img height="48" width="48" class="img-fluid rounded-circle" src="<?php echo "$avatar"; ?>">
<div class="text-secondary"><?php echo $name; ?></div>
</td>
<td><a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a></td>
<td>
<div class="dropdown dropleft text-center">