Hi, today We are going to tell you about delete image from folder in php how to remove the file or picture from folder as well as MySQL database Table using php script, many developers they are not care to delete a picture or file while deleting a records from database they keep the files related to records and it will effects on server storage, we will find many solutions to remove files from directory We will give you a simple process let’s display how we do this.
As a PHP beginner step by step, remove file from folder as well as database can be very hard. Because it required Two types of the processes. First of all is remove file from directory. Second is delete database record having that file path.
PHP Remove File from Folder and Database
Developer often confused which process comes starting. few delete database record initial and then remove/delete file from directory as well as many remove/delete file primary and then delete database record. Therefor which practice is correct?
Best solutions is to remove/delete file from directory first(delete uploaded file php) and then delete MySQL database table record by php delete image on click. In this tutorial We are going to Display you php unlink image from folder with how to do that.
here simple step to DB connect this file to mysql database as well as get or fetch the records from gallery DB table bellow We are just getting single record only if you want you can retrieve all Database records and keep it in while loop, now We are just location the image tag as well take the src anchor tag for delete button as well as location the do_file_delete.php file in href with We just pass arguments the gallery id to customproductid variable
Create database demo;
Create Database Table:
CREATE TABLE `galleries` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`image` VARCHAR(255) NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
success KEY (`id`)
here connect this file to database, now retrieve the image from gallery table by using where query condition and assign the $del variable to galleryid, now use unlink() method to remove/delete the files or pictures from folder unlink(“medias/$fetchImgTitleName”); inside the function just give the directory path and file name to remove/delete after the function write the query to delete the gallery record with delete previous image from directory when updated by new image in php also you can read my previous post Like as how to delete image from folder in php codeigniter
$querySelect = "select * from galleries where id = ".$_GET['deleteid'];
$ResultSelectStmt = mysqli_query($conn,$querySelect);
$fetchRecords = mysqli_fetch_assoc($ResultSelectStmt);
$fetchImgTitleName = $fetchRecords['image'];
$createDeletePath = "uploads/".$fetchImgTitleName;
$liveSqlQQ = "delete from galleries where id = ".$fetchRecords['id'];
$rsDelete = mysqli_query($conn, $liveSqlQQ);
$displayErrMessage = "Unable to delete Image";
Complete Code : delete image from database and folder in php
I hope you get an idea about delete image from folder in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.