John Davidson

php - laravel - delete multiple photos with unlink path wont work

0 comments
Message:


What I am trying to achieve is to delete multiple photos from one delete command, but it only delete the record from the database, but unable to delete the file in my local storage. I am trying to use unlink() method in order to make this to work. I hope somebody would enlighten me where that I made my mistake. Thank you!


Delete method



//There are 8 columns of image, the database record is wiped, but not in the local storage.

public function destroy(Gallery $gallery){

for ($i = 1; $i < 9; $i++) {
$path = storage_path('app/public/' . $gallery->image_ . $i);

if (file_exists($path)) {
@unlink($path);
}
}

$gallery->delete();

}


My Answer:
<= 8; $i++){__ $imagePath = public_path('images/' . $gallery->{"image" . $i});__ if (file_exists($imagePath)) {__ unlink($imagePath);__ }__ }__ $gallery->delete();__ return redirect()->route('gallery.index')->with('success', 'Gallery deleted successfully');__ }

Rate this post

4 of 5 based on 6149 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us