PHP Remove File from Folder and Database | Lessoncup Programming Blog

Thursday, 3 April 2014

PHP Remove File from Folder and Database

Hi, today i am going to tell you about how to remove the file or picture from folder and database using php, some 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 folder i will give you a easy process let's see how we do this.


PHP

here connect this file to database and retrieve the records from gallery table here i am just retrieving one record only if you want you can access all records and keep it in while loop, now i am just place the image tag and take the anchor tag for delete button and place the delete.php file in href and i just pass the gallery id to del variable
<div id="box">

<?php
$sql=mysql_query("select * from gallery limit 1");
$row=mysql_fetch_array($sql);
?>

<img src="images/<?php echo $row['image']?>" />

<p></p>

<a href="delete.php?del=<?php echo $row['galleryid']?>" class="link">Delete</a>

</div>

delete.php

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 the files or pictures from folder unlink("images/$row[image]"); inside the function just give the folder path and file name to remove after the function write the query to delete the record
<?php
extract($_REQUEST);
include('db.php');

$sql=mysql_query("select * from gallery where galleryid='$del'");
$row=mysql_fetch_array($sql);

unlink("images/$row[image]");

mysql_query("delete from infogall where infoid='$del'");

header("Location:index.php");

?>

Live Demo   Download Script

10 comments :

  1. khasim bai plz share the code of add and cancel friends request like facebook plz

    ReplyDelete
  2. good post bro
    thanks from http://www.blogonmind.com

    ReplyDelete
  3. thanks broo
    very helpfull for me :)

    ReplyDelete
  4. Where did you assign a value to variable 'del'?
    I think you want it like this:

    $del = intval($_GET['id']);

    ReplyDelete
  5. mehn u are a real genious ,worked perfectly, god bless INDIAN gurus like you

    ReplyDelete
  6. Thank You so Much very easy code

    ReplyDelete
  7. only remove value from database but not delete file from folder. why.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons