more on portfolio









November 2008
M T W T F S S
« May «-»  
 12
3456789
10111213141516
17181920212223
24252627282930






 


How to: Delete a folder with all files inside

Log entry of August 28th 2007 05:26:08 pm
Filed under: PHP, Snippets
« How to: Prompt out the ‘SAVE AS’
How to: Load a template in *.TPL »

We all know we can find a PHP code to delete a directory, but to delete a directory will thousands and millions of files in it? Here’s the coding which is quite a hassle to find, and I’m sharing it with y’all.
function del_dir($dir)
{
$res = @opendir($dir);if(!$res) return;

	while(($file = readdir($res)) !== false)
	{
		if($file !== '.' && $file !== '..')
		{
			$f = $dir . '/' . $file;

			if(is_dir($f))
			{
			del_dir($f);
			}
			else
			{
			@unlink($f);
			}
		}
	}

closedir($res);
@rmdir($dir);
}
Save the code above to a single PHP file (eg: deleteall.php). Then on the file where you want to execute this function, place this code.
$dir = "FOLDER_DIRECTORY";
include('deleteall.php');
del_dir($dir);
…And viola! But always remember that there should be a prompt before the the code is execute. Otherwise, you end up mistakenly deleting the wrong stuff!






Leave a Reply