Skip to content

Commit

Permalink
"update: delete profile picture"
Browse files Browse the repository at this point in the history
  • Loading branch information
didof committed Jan 15, 2020
1 parent cb0b444 commit 29abb53
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 5 deletions.
6 changes: 6 additions & 0 deletions changePic.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
?>
<p>Click below to select a picture from your pc.</p>

<!-- Upload/change picture -->
<form action="includes/changePic.inc.php" method="post" enctype="multipart/form-data">
<!-- enctype specifica come il data del form dovrebbe essere codificato -->
<input type="file" name="file"><br>
<button type="submit" name="pic-submit">Upload picture</button>
</form>

<!-- Delete picture -->
<form action="includes/deletePic.inc.php" method="post">
<button type="submit" name="pic-delete">Delete picture</button>
</form>

<?php
if (!empty($_GET["upload"])) {
switch ($_GET["upload"]) {
Expand Down
6 changes: 6 additions & 0 deletions displayError.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
case "login":
echo 'You are logged in as ' . $_SESSION["userName"] . ', by your e-mail ' . $_SESSION["userMail"];
break;
case "delete":
echo 'Your profile image was deleted successfully.';
break;
}
}
} else if (!isset($_SESSION["userId"])) { # ciò che vedi durante logout
Expand Down Expand Up @@ -45,6 +48,9 @@
case "goAway":
echo 'You tried to reach a page from the URL.';
exit();
case "deleteDefault":
echo 'You are not allowed to delete the default profile image.';
exit();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions includes/changePic.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
$fileNewName = "profile_image_user_" . $userId . '.' . $fileExtention;
echo '$fileNewName => ' . $fileNewName . '<br><br>';


# Update in the db
include_once "dbh.inc.php";
$newStatus = 1;
Expand Down
78 changes: 78 additions & 0 deletions includes/deletePic.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

include_once "dbh.inc.php";
session_start();

$userId = $_SESSION["userId"];
echo '$userId => ' . $userId . '<br><br>';

# First of all, I assess if the pic was already deleted
$sql = "SELECT * FROM profileimg WHERE userId=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "i", $userId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
var_dump($row);
if (!($row > 0)) {
# Pic not present
echo '>Error: pic is not present.<br><br>';
} else if ($row["status"] == 0) {
echo '>Error: You are not allowed to cancel the default pic.';
echo '<img src="uploads/default_profile_img.jpg" height="100px" width="100px">';
header("Location: ../index.php?error=deleteDefault");
exit();
} else {

$fileName = "../uploads/profile_image_user_" . $userId . "*";
# the * means that there is something after that.
echo '$fileName => ' . $fileName . '<br><br>';

$fileInfo = glob($fileName, GLOB_NOCHECK);
# glob() is a function that search for a specific file that as part of the name
# il flag GLOB_NOCHECK fa riportare il path nel caso in cui non trovi nulla
echo 'glob($fileName) => ' . $fileInfo[0];
// var_dump($fileInfo);
echo '<br><br>';
echo '>Check:<br> <img src="' . $fileInfo[0] . '" height="30px" width="30px"><br><br>';

# Beware
// Suppose you are using Id = 1
// the path will be ../uploads/profile_image_user_1.jpg
// BUT ALSO ../uploads/profile_image_user_11.jpg, ...profile_image_user_16.jpg and, etc.
# FIX
// just work on the first index, the lowest than the correct one
$fileExploded = explode(".", $fileInfo[0]);
echo '$fileExploded => ';
var_dump($fileExploded);
echo '<br><br>';

$fileExt = end($fileExploded);
echo '$fileExt => ';
var_dump($fileExt);
echo '<br><br>';
// note: "end()" select the last value in array, while "current()" the first one

$fileNamePath = "../uploads/profile_image_user_" . $userId . "." . $fileExt;
echo '$fileNamePath => ';
var_dump($fileNamePath);
echo '<br><br>';

# Delete file
if (!unlink($fileNamePath)) {
echo '>Error: file was not deleted.';
} else {
# Cancel from database too
$sql = "UPDATE profileimg SET name=?, status=? WHERE userId=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
$default = "default_profile_img.jpg";
$status = 0;
mysqli_stmt_bind_param($stmt, "sbi", $default, $status, $userId);
mysqli_stmt_execute($stmt);
echo '>Success: file was deleted from repository and overdrived in db.';
header("Location: ../index.php?success=delete");
exit();
}
}
14 changes: 9 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
echo '<img src="uploads/default_profile_img.jpg" height="100px" width="100px">';
} else {
# this, the user already uploaded the pic
echo "<img src='uploads/" . $row["name"] . "' height='100px' width='100px'>";
echo "<br>trakPath => 'uploads/" . $row["name"] . "'";
echo "<img src='uploads/" . $row["name"] . "?" . mt_rand() . "' height='150px' width='200px'>";
// some browser can remember the picture and this force the user to refresh the page
# FIX -> add mt_rand()
// <img src='uploads/profile_image_user_1.jpg?218728917'>
}
}
}
Expand All @@ -46,19 +48,21 @@
</div>
<div class="infoUser">
<h3>Prostagma executed:</h3>
<?php require "displayError.php"; ?>
<?php require "displayError.php" ?>

</div>
<div class="canDo">
<h3>Things you can do:</h3>
<ul>
<li><a href="changePwd.php">Change password</a></li>
<li><a href="changePic.php">Change profile picture</a></li>
<li><a href="changePic.php">Set/unset profile picture</a></li>
</ul>
</div>
<div class="workingOn">
<h3>Working on:</h3>
<ul>
<li><a href="deleteUser.php">Delete account</a></li>
<li><a href="uploadPic.php">Upload picture</a></li>
<li><a href="changeMail.php">Change e-mail</a></li>
<li><a href="changeUsername.php">Change username</a></li>
</ul>
Expand All @@ -81,7 +85,7 @@
</ul>
</div>
<div class="photos">
show photos.
Show photos.
</div>
</div>
<?php
Expand Down
4 changes: 4 additions & 0 deletions style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ a {
body {
background-color: #bfd8d2;
}
li {
list-style: none;
}

.grid {
display: grid;
Expand All @@ -20,6 +23,7 @@ body {
"workingOn photos photos photos empty"
"wantTo photos photos photos empty"; */
background-color: #fedca2;
padding: 10px;
}

.empty {
Expand Down
Binary file removed uploads/profile_image_user_44.jpg
Binary file not shown.
Binary file removed uploads/profile_image_user_45.jpg
Binary file not shown.
Binary file removed uploads/profile_image_user_47.jpg
Binary file not shown.
Binary file removed uploads/profile_image_user_48.jpg
Binary file not shown.
Binary file removed uploads/profile_image_user_49.jpg
Binary file not shown.
Binary file removed uploads/profile_image_user_50.jpg
Binary file not shown.
File renamed without changes

0 comments on commit 29abb53

Please sign in to comment.