Call us on 078 24 35 85 44 / e: info@thescube.com
logo


PHP Delete Directory Recursively

Most of the occasions we might face a scenario on deleting a directory and all its contents subdirectories. So I came up with a simple function to acheive this issue. This function uses php’s inbuilt functions such as file_exits, is_dir, is_link, unlink, scandir and rmdir.

update: Incase you are facing trouble using  unlink() and when you get an error saying warning  Is dir. Also if you get error permission denied in windows xp. The below code will work for these issues.

$dir = ‘your_dir/’; // example.

function removeDir($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 (!removeDir($dir . “/” . $item))
{

if (!removeDir($dir . “/” . $item)) return false;
};
}
return rmdir($dir);
}

Author: Srini

Website: Dreamsource

VN:F [1.9.17_1161]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)

You might be interested in:

  1. Joy
    I will probably end up being back to look at some other posts that you have another time.
  2. shopping time
    I think it is a bit complicated but I've liked.
  3. fashion stylist
    your idea helped me in clearing my questions.... thanks

Leave a Reply

*

captcha *

Login with Facebook: