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 PHP.net 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:

/data/123/images/<image_name>.jpg
/data/123/moreimages/<image_name>.jpg

Could someone point us in the right direction please?

Many thanks.
<?php
function init_delete() {
		self::fetch_info('admin');
		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 == '..') {
					continue;
				}
				$file = str_replace('//','/',$path.'/'.$filename);
			}
			if(!@rmdir($path)) {
				return false;
			} else {
				return true;
			}
		}
	}
?>

Open in new window

DReade83Asked:
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.

hernst42Commented:
I suggest using PEAR to delete directory with subdirectories like:
http://pear.php.net/manual/en/core.pear.system.rm.php

require_once('System.php');
// make sure you have a backup before trying the code and check that $_GET['id'] is never empty
System::rm($path);
0

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))
  @rmdir($path)

0
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).
0
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.

cyberwebservice
Experts Exchange Cleanup Volunteer
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
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
PHP

From novice to tech pro — start learning today.