store profile picture in database when uploaded

This commit is contained in:
Thomas Rubini 2023-01-23 18:55:13 +01:00
parent 5cf511a9c9
commit a4fcfb08e3
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
3 changed files with 29 additions and 2 deletions

View File

@ -111,6 +111,25 @@ final class UserController
$O_userModel = new UserModel();
// TODO harmonize error handling here
if (isset($_FILES["profilPicture"])) {
if ($_FILES['profilPicture']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['profilPicture']['error']);
}
$info = getimagesize($_FILES['profilPicture']['tmp_name']);
if ($info === false) {
die("Unable to determine image type of uploaded file");
}
if (($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
die("Not a jpeg/png");
}
$fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
$O_userModel->updateProfilePicByID($_SESSION["ID"], $fp);
}
if (isset($_POST["email"])) {
$S_email = $_POST["email"];
if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
@ -124,7 +143,7 @@ final class UserController
}
}
header("Location: /user");
// header("Location: /user");
}
public function deleteAction(Array $A_urlParams = null, Array $A_postParams = null)

View File

@ -58,6 +58,14 @@ final class UserModel extends UserSessionModel
return $row["USERNAME"];
}
public function updateProfilePicByID($I_id, $profile_pic_fp){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET PROFILE_PIC=:profile_pic WHERE ID=:id");
$stmt->bindParam("id", $I_id);
$stmt->bindParam("profile_pic", $profile_pic_fp, PDO::PARAM_LOB);
$stmt->execute();
}
public function updateEmailByID($I_id, $S_newEmail){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:new_email WHERE ID=:id");

View File

@ -9,7 +9,7 @@
<a href="/user/logout">Se déconnecter</a>
<form action="/user/update" method="post">
<form action="/user/update" method="post" enctype="multipart/form-data">
<label for="profilPicture">Changer l'image de profil&nbsp;</label>
<input type="file" name="profilPicture" id="profilPicture" accept="image/*">