Delete all files or directories from specified path using php


Please find below code for remove files and directories using PHP.

$folder = “test/test2/test3”; or $folder = “test/test.txt”;
deleteDirectory($folder);
function deleteDirectory($dir) {
if (!file_exists($dir)) { return true; }
if (!is_dir($dir) || is_link($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == ‘.’ || $item == ‘..’) { continue; }
if (!deleteDirectory($dir . “/” . $item, false)) {
chmod($dir . “/” . $item, 0777);
if (!deleteDirectory($dir . “/” . $item, false)) return false;
};
}
return rmdir($dir);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s