Deleting all files and folders within a folder using PHP

I need a PHP script that can be run as part of a page which deletes all related SQL entries and files located on the server. The script in the snippet was partly written by me and another part (the deletion of files) copied from one of the examples on the website.

The function only deletes the DB entries, but doesn't delete what's in the /data/ folder.

The Data folder is organised as follows:


Could someone point us in the right direction please?

Many thanks.
function init_delete() {
		if(!isset($_GET['id'])) {
			include('admin/http404.php'); exit();
		} else {
			$path	=	'data/'.$_GET['id'].'/';
			mysql_query('DELETE FROM Data WHERE DataId='.$_GET['id']);
			if(!file_exists($path)) {
				return false;
			$files = scandir($path);
			foreach($files as $filename)
				if($filename == '.' || $filename == '..') {
				$file = str_replace('//','/',$path.'/'.$filename);
			if(!@rmdir($path)) {
				return false;
			} else {
				return true;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I suggest using PEAR to delete directory with subdirectories like:

// make sure you have a backup before trying the code and check that $_GET['id'] is never empty

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Loganathan NatarajanLAMP DeveloperCommented:
you must check whether the dir is exist or not... then it wil work

if  (is_dir($path))

DReade83Author Commented:
Thanks for the feedback guys. I'll let you know the outcome when I'm able to do some testing (in a few days).
CWS (haripriya)Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split: hernst42 {http:#20797166} & logudotcom {http:#20798232}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Experts Exchange Cleanup Volunteer
Forced accept.

EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.