I have written my own function that knows how to delete the content of a non-empty folder. They function recursively deletes the content of all the files and subfolders of the function.
The problem is that when I run this function on a folder which contains numerous subfolders anf files, it runs for ~40 seconds before finishing successfully.
When I let Windows Explorer do the same thing, it completes the job in less than 2 seconds.
What could be so inefficient in my code? or maybe its the way PHP is parsed?
Here is my function:
Code: Select all
function deleteFolder($folder, $enableRecursiveDelete = true) {
if ($dirHandle = opendir($folder)) {
while (($file = readdir($dirHandle)) !== false) {
$currentEntry = $folder . $file;
if (is_file($currentEntry)) {
if (!unlink($currentEntry)) {
// cannot delete a file
}
}
else if (is_dir($currentEntry) && $enableRecursiveDelete) {
if ($file == '.' || $file == '..') {
continue;
}
deleteFolder($currentEntry . "/", true);
}
}
closedir($dirHandle);
}
else {
// cannot obtain a refernce to the folder
}
if (!rmdir($folder)) {
// cannot delete the now empty folder
}
}