Deleting all files and folders within a folder using PHP

Posted on 2008-02-01
Medium Priority
Last Modified: 2012-08-13
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:


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

Question by:DReade83
LVL 48

Accepted Solution

hernst42 earned 1000 total points
ID: 20797166
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
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 1000 total points
ID: 20798232
you must check whether the dir is exist or not... then it wil work

if  (is_dir($path))


Author Comment

ID: 20798928
Thanks for the feedback guys. I'll let you know the outcome when I'm able to do some testing (in a few days).
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21159649
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

Expert Comment

ID: 21197322
Forced accept.

EE Admin

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question