Tutorials Home Scripts Home

Empty Directories from the 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);
}
}

Category: Website Programming | Views: 1,553 | on: September 24, 2009 | by: jooria
Enjoyed the article? Subscribe and get the new Tutorials

Enter your email address:

Last 1 Responses

Comment form

  1. Emraan About 2 months ago
    Testing

Leave a Comment

Comment form