Friday, 26 June 2015

PHP: Delete Uploaded Files From Folder using PHP and MySQL

This is my continuation post of how to upload and view files , In this post we will see that how to completely delete(remove) uploaded files from folder and MySql database table using PHP , you can delete any files pdf, mp3, image, video any...,when you upload any file it moves on particular folder, after delete  that database record data only deleted but files stay on same folder only it's not delete. So how to do let's see.

Here i am going to use same old Database crediantials which are used in my file uploading tutorial.
using unlink() function you 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.

0 comments:

Post a Comment