PHP

Empty Directory And delete the templete engines cached files
Empty Directories from the files

Definition

This function depends on The unlink() function.

This quick way to delete any directory or any files.

you can use this in Many areas such as delete the templete engines cached files.

Syntax:

empty_dir($directory,$delit);

Parameters

directory

Path to the directory.

delit

if true delete also $directory, if false leave it alone.Usage:

1-need to empty a directory, but keeping it.Use this

empty_dir('Path/directory', false);

2-need to empty a directory,and delete it too.Use this

empty_dir('Path/directory', ture);

tip: To unlink, the web server user must have write permissions to the directory.

Conversely, if a user has write permissions to a directory, it may delete files from that directory regardless of who owns them

The function

function empty_dir($directory, $delit) {
if(!$dh = @opendir($directory)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($directory.'/'.$obj)) empty_dir($directory.'/'.$obj, true);
}
closedir($dh);
if ($delit){
@rmdir($directory);
}
}
in: » Website Programming » PHP | Posted on Sep 24th, 2009 | views 2,481
About the author
i'm moustafa from egypt i love doing one thing 'web programming & designing', This year I've got 19 years old and i'm in the english College of management (in Business Administration part soon)