As a PHP beginner, remove file from folder and database can be difficult. Because it involves 2 processes. First is remove file from folder. Second is delete database record having that file path.
Developer often confused which process comes first. Some delete database record first and then remove file from folder and some remove file first and then delete database record. So which practice is correct?
Best way is to remove file from folder first and then delete database record. In this tutorial I am going to show you how to do that. Now let’s start.
Also Read: How to upload multiple images in PHP and store in Mysql
Create Database:
1 2 3 |
Create database demo; |
Create Database Table:
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE TABLE `images` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `image` VARCHAR(255) NOT NULL, `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=10 ; |
Database Connection and Select Images Query:
1 2 3 4 5 6 7 8 9 10 11 12 |
$conn = mysqli_connect('localhost','root','','demo'); if(!$conn) { die(mysqli_error()); } $sql = "select * from images"; $rs = mysqli_query($conn, $sql); |
Display Images from Database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<div class="container"> <h3 class="text-center">Delete Images</h3> <div class="row"> <?php while($row = mysqli_fetch_assoc($rs)) { $imgWithPath = "uploads/".$row['image']; ?> <div class="col-md-12 text-center" style="margin-top:10px"> <img src="<?php echo $imgWithPath ?>" width="200"> <a href="?deleteid=<?php echo $row["id"]?>" class="btn btn-primary">Delete</a> </div> <?php } ?> </div> </div> |
Success and Error Messages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php if(isset($errorMsg)) { ?> <div class="alert alert-danger"> <?php echo $errorMsg; unset($errorMsg); ?> </div> <?php } ?> <?php if(isset($_GET['success']) && $_GET['success'] == 'true') { ?> <div class="alert alert-success"> <?php echo "Images has been deleted sucessfully"; ?> </div> <?php } ?> |
Remove Image Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php if(isset($_GET['deleteid'])) { $selectSql = "select * from images where id = ".$_GET['deleteid']; $rsSelect = mysqli_query($conn,$selectSql); $getRow = mysqli_fetch_assoc($rsSelect); $getIamgeName = $getRow['image']; $createDeletePath = "uploads/".$getIamgeName; if(unlink($createDeletePath)) { $deleteSql = "delete from images where id = ".$getRow['id']; $rsDelete = mysqli_query($conn, $deleteSql); if($rsDelete) { header('location:index.php?success=true'); exit(); } } else { $errorMsg = "Unable to delete Image"; } } ?> |
All Code Together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
<?php $conn = mysqli_connect('localhost','root','','demo'); if(!$conn) { die(mysqli_error()); } $sql = "select * from images"; $rs = mysqli_query($conn, $sql); if(isset($_GET['deleteid'])) { $selectSql = "select * from images where id = ".$_GET['deleteid']; $rsSelect = mysqli_query($conn,$selectSql); $getRow = mysqli_fetch_assoc($rsSelect); $getIamgeName = $getRow['image']; $createDeletePath = "uploads/".$getIamgeName; if(unlink($createDeletePath)) { $deleteSql = "delete from images where id = ".$getRow['id']; $rsDelete = mysqli_query($conn, $deleteSql); if($rsDelete) { header('location:index.php?success=true'); exit(); } } else { $errorMsg = "Unable to delete Image"; } } ?> <!DOCTYPE html> <html> <head> <title>PHP remove uploaded file from folder and database</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" > </head> <body> <div class="container"> <h3 class="text-center">Delete Images</h3> <div class="row"> <?php if(isset($errorMsg)) { ?> <div class="alert alert-danger"> <?php echo $errorMsg; unset($errorMsg); ?> </div> <?php } ?> <?php if(isset($_GET['success']) && $_GET['success'] == 'true') { ?> <div class="alert alert-success"> <?php echo "Images has been deleted sucessfully"; ?> </div> <?php } ?> <?php while($row = mysqli_fetch_assoc($rs)) { $imgWithPath = "uploads/".$row['image']; ?> <div class="col-md-12 text-center" style="margin-top:10px"> <img src="<?php echo $imgWithPath ?>" width="200"> <a href="?deleteid=<?php echo $row["id"]?>" class="btn btn-primary">Delete</a> </div> <?php } ?> </div> </div> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> |