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.
Create Database:
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`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=10 ;
Database Connection and Select Images Query:
$conn = mysqli_connect('localhost','root','','gallery_demo'); if(!$conn) { die(mysqli_error()); } $sql = "select * from galleries"; $rs = mysqli_query($conn, $sql);
Display Images from Database:
Delete Images