Pages

Search This Blog

Saturday 20 October 2012

How we delete folder and all files in PHP


<?php
/**
*
* @author Zuh@ir Mirza <zuhair_mirza@yahoo.com>
* @param string $dirname Directory to delete
* @return bool Returns TRUE on success, FALSE on failure
*/
function smartDelete($dirname)
{
// Sanity check
if (!file_exists($dirname)) {
return false;
}

// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}

// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Recurse
smartDelete($dirname.DIRECTORY_SEPARATOR.$entry);
}

// Clean up
$dir->close();
return rmdir($dirname);
}

$dir_path = 'F:\xampp\htdocs\dev\test\ip_com\delete';
echo smartDelete($dir_path);

?>

No comments:

Post a Comment