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

You might be interested in :

  1. ★Fancy ♥Symbols for ►Facebook, ♪Myspace Want to put fancy symbols on your facebook or...
  2. What font is being used? If you are like us at thescube, you might...
  3. Wordpress Post Body Content Not Displaying Having problem loading wordpress page or post content? Deactivation...
  4. Windows Live Mail (WLM) – minimized to system tray Wondering how to minimize windows live mail to system...
  5. Photoshop Stuck on hand tool or icon Adobe Photoshop CS3 / CS2 / CS or any...

Tags: dreamsource, php

Leave a Reply

Get in touch! get in touch

Shovan Sargunam @ Birmingham
t: 0 78 24 35 85 44
e: info@thescube.com


lo_tm-logo3 t5_dacapo-college