Skip to content

Commit 9b63ad9

Browse files
committed
Fixed duplicate uploaded images
1 parent 97299ae commit 9b63ad9

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

app/Http/Controllers/UserController.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,13 @@ public function editPage(Request $request)
814814
]);
815815

816816
if ($request->hasFile('image')) {
817+
818+
// Delete the user's current avatar if it exists
819+
while (findAvatar($userId) !== "error.error") {
820+
$avatarName = findAvatar($userId);
821+
unlink(base_path($avatarName));
822+
}
823+
817824
$fileName = $userId . '_' . time() . "." . $profilePhoto->extension();
818825
$profilePhoto->move(base_path('assets/img'), $fileName);
819826
}
@@ -866,10 +873,12 @@ public function themeBackground(Request $request)
866873
}
867874
}
868875

869-
if (file_exists(base_path('assets/img/background-img/') . $pathinfo)) {
870-
File::delete(base_path('assets/img/background-img/') . $pathinfo);
876+
// Delete the user's current background image if it exists
877+
while (findBackground($userId) !== "error.error") {
878+
$avatarName = "assets/img/background-img/" . findBackground(Auth::id());
879+
unlink(base_path($avatarName));
871880
}
872-
881+
873882
$fileName = $userId . '_' . time() . "." . $customBackground->extension();
874883
$customBackground->move(base_path('assets/img/background-img/'), $fileName);
875884

@@ -1152,6 +1161,12 @@ public function importData(Request $request)
11521161
if (in_array($userExtension, $allowedExtensions)) {
11531162
// Decode the image data from Base64
11541163
$imageData = base64_decode($userData['image_data']);
1164+
1165+
// Delete the user's current avatar if it exists
1166+
while (findAvatar(Auth::id()) !== "error.error") {
1167+
$avatarName = findAvatar(Auth::id());
1168+
unlink(base_path($avatarName));
1169+
}
11551170

11561171
// Save the image to the correct path with the correct file name and extension
11571172
$filename = $user->id . '.' . $userExtension;
@@ -1161,7 +1176,7 @@ public function importData(Request $request)
11611176
$user->image = $filename;
11621177
}
11631178
}
1164-
1179+
11651180
$user->save();
11661181

11671182
// Delete all links for the authenticated user

0 commit comments

Comments
 (0)