Q:

Delete files then directory

I have this so far:
<?php

$path = "files/";

$files = glob("" . $path . "{*.jpg, *.gif, *.png}", GLOB_BRACE);

$i = 0;

foreach($files as $file)
{
    $delete = unlink($file);

    if($delete)
    {
        echo $file . " deleted!<br />";
        $i - 1;
    }
    else
    {
        echo $file . " could not be deleted...<br />";
        $i + 1;
    }   
}

if($i == 0)
{   
    if(is_dir($path))
    {
        $remove = rmdir($path);

        if($remove)
        {
            echo "directory was deleted</br />";
        }
        else
        {
            echo "directory could not be deleted</br />";
        }
    }
    else
    {
        echo "not a valid directory<br />";
    }
}
else
{
    echo "there are some files in the folder";
    echo $i;
}

?>

It deletes every file, which is great. However, it doesn't remove the directory. What's wrong with this?

A:

You need to pull the rmdir out of the loop. Something like:
$numfailed = 0;

foreach($files as $file)
{
    $delete = unlink($file);

    if($delete)
    {
        echo $file . " deleted!<br />";
    }
    else
    {
        echo $file . " could not be deleted...<br />";
        $numfailed++;
    }   
}

if($numfailed == 0)
{   
    if(is_dir($path))
    {
        $remove = rmdir($path);

        if($remove)
        {
            echo "directory was deleted</br />";
        }
        else
        {
            echo "directory could not be deleted</br />";
        }
    }
    else
    {
        echo "not a valid directory<br />";
    }
}
else
{
    echo "there are still files in the folder, failed to remove $numfailed";
}

