Delete Uploaded Files from Folder using PHP and MySQL | Coding Cage

Delete Uploaded Files from Folder using PHP and MySQL

By
Hello friends, this is ours continuation post of previous how to upload and view files, now In this tutorial we will see that how to completely delete(remove) uploaded files from folder as well as from MySQL database table using PHP, you can delete any files pdf, mp3, image, video etc. when you upload any file it moves on particular folder and it's necessary to update or delete files from folder as well when you delete data from mysql. so let's have a look at this simple yet useful tutorial.
Delete Uploaded Files From Folder using PHP and MySQL


Read Also : Upload Insert, Update & Delete operations with Image [PDO Query]

Here i am going to use the same old Database and table with dumping data which have been used in ours file uploading tutorial.

In PHP using unlink() function we can delete files from folder.

example :
unlink("folder_name/".file_name);

Sample Code :

$res=mysql_query("SELECT file FROM tbl_uploads WHERE id=".$_GET['remove_id']);
$row=mysql_fetch_array($res);
mysql_query("DELETE FROM tbl_uploads WHERE id=".$_GET['remove_id']);
unlink("uploads/".$row['file']);

Script explained :
- First write select query to fetch file from database.
- This code can be work with if(isset($_GET[]) condition.
- After fetching file put the fetched file into the unlink() function like above.

That's it guys, hope you like this short and simple tutorial.




21 comments:

  1. good script
    thank you for sharing..

    ReplyDelete
  2. Thanks for sharing, codes work for me

    ReplyDelete
  3. unlink("uploads/".$row['file']); how to delete file from the folder

    ReplyDelete
  4. unlink("uploads/".$row['file']); how to delete file from the particular folder

    ReplyDelete
    Replies
    1. give the particular folder name in "unlink()" function to delete files...

      Delete
  5. Sir how I store music file in server folder and show in web page for user download.

    ReplyDelete
    Replies
    1. file downloading is another part, i will post a tutorial for file download soon...

      Delete
  6. Thanks sir .This code help me ..

    ReplyDelete
  7. MySQL is deprecated though, can you make on in MySQLi or PDO please?

    ReplyDelete
  8. this script is not download ???

    ReplyDelete
  9. I can't download script :(

    ReplyDelete
  10. Hi ,
    i want to insert data in database when image file choose else not
    so what we have to try there

    ReplyDelete
  11. hi,
    i want to insert image path in data base is image file selected else not
    so, what i have to there ?
    any one guide me please :)

    ReplyDelete
  12. Nice.. works like charm

    ReplyDelete
  13. Hi.. Can you help me auto upload files to xampp using php?

    ReplyDelete