views:

105

answers:

5

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: 

rmdir removes a directory, but only if it is empty. You have to delete each file (and each subdirectory with their files) before a directory can be removed.

jmz
I know that. I only allow images in that folder, so every file is deleted but it doesn't remove the directory when there is no files in the folder...
YouBook
So why not just `glob("$path*");`? And drop the `$i == 0` check: it's useless if you're deleting everything.
jmz
+2  A: 

You're trying to remove your directory in the foreach-loop which will delete the files inside that directory.

I'd try deleting all files first and then delete the directory, otherwise it won't be empty and can't be deleted.

Also you $i-counter won't do the job of telling you when the directory is empty: imagine your first file will be deleted, then $i = -1. If now your second file is not deleted, your $i = 0 ... which is the condition to delete the directory, even though it's not empty because at least your second file remains.

Select0r
Oh yeah, I noticed that, silly me. I changed the code now. Still won't be able to delete the directory. +1
YouBook
Your `if($i == 0)` will still not tell you if your directory is empty, see my example above and mvds code using "numfailed" to see if a file couldn't be deleted.
Select0r
A: 

possibly permissions

as you only deleting certain certain filetypes there may be other files left in the directory that you do not have permissions to delete, there for you cannot delete the folder.

try checking if the folder is empty before trying to rmdir command on it.

RobertPitt
+1  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";
}
mvds
A: 

Check the examples at: http://php.net/manual/en/function.rmdir.php

Eduardo