this is modified function try with this. this will delete all file except *.php
OR *.PHP
files
function rrmdir($dir) {
$structure = glob(rtrim($dir, "/").'/*');
$rm_dir_flag = true;
if (is_array($structure))
{
foreach($structure as $file)
{
if (is_dir($file))
{
rrmdir($file);
}
else if(is_file($file))
{
$ext = substr($file, -4);
if($ext==".php" || $ext==".PHP")
{
$rm_dir_flag = false;
}
else
{
unlink($file);
}
}
}
}
if($rm_dir_flag)
{
rmdir($dir);
}
}
UPDATE 2:
if you want to protect file with particuar extension you can do this
rrmdir($your_directory, ".php");
//or
rrmdir($your_directory, ".pdf");
//or
rrmdir($your_directory, ".jpeg");
function rrmdir($dir, $protect_extension) {
if(!is_dir($dir))
{
return;
}
$len = strlen($protect_extension)*(-1);
$structure = glob(rtrim($dir, "/").'/*');
$rm_dir_flag = true;
if (is_array($structure))
{
foreach($structure as $file)
{
if (is_dir($file))
{
rrmdir($file, $protect_extension);
}
else if(is_file($file))
{
$ext = substr($file, $len);
if($ext==$protect_extension || $ext==strtoupper($protect_extension))
{
$rm_dir_flag = false;
}
else
{
unlink($file);
}
}
}
}
if($rm_dir_flag)
{
rmdir($dir);
}
}