thom
thom

Reputation: 41

Recursive delete

I have this code to recursive delete files and directories. It works fine but has a little problem. If $path = /var/www/foo/ it will delete everything inside of foo, but not foo. I want to delete foo directory too. Any idea?

public function delete($path) {
    if(!file_exists($path)) {
        throw new RecursiveDirectoryException('Directory doesn\'t exist.');
    }

    $directoryIterator = new DirectoryIterator($path);

    foreach($directoryIterator as $fileInfo) {
        $filePath = $fileInfo->getPathname();

        if(!$fileInfo->isDot()) {
            if($fileInfo->isFile()) {
                unlink($filePath);
            }
            else if($fileInfo->isDir()) {
                if($this->emptyDirectory($filePath)) {
                    rmdir($filePath);
                }
                else {
                    $this->delete($filePath);
                    rmdir($filePath);
                }
            }
        }
    }
}

Upvotes: 4

Views: 6789

Answers (4)

user1679267
user1679267

Reputation: 77

try this

unset( $directoryIterator); rmdir($filePath);

Upvotes: 0

Bobby
Bobby

Reputation: 11576

You're missing one last rmdir. You could either call it after $this->delete($path) like this:

$this->delete($path);
rmdir($path);

Or you could change the foreach-loop like this:

public function delete($path) {
    //snip

    foreach($directoryIterator as $fileInfo) {
        //snip
                else {
                    $this->delete($filePath);
                }
            }
        }
    }

    rmdir($path);
}

Also, I sure hope you validate what paths you get there, if this is visible to a user (like a "Delete Everything On My Webspace"-function. I mean, you'll have a lot of fun if somebody passes /etc/ into there.

Upvotes: 3

ircmaxell
ircmaxell

Reputation: 165201

Why even recurse in your function?

public function delete($path) {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path),
        RecursiveIteratorIterator::CHILD_FIRST
    );
    foreach ($it as $file) {
        if (in_array($file->getBasename(), array('.', '..'))) {
            continue;
        } elseif ($file->isDir()) {
            rmdir($file->getPathname());
        } elseif ($file->isFile() || $file->isLink()) {
            unlink($file->getPathname());
        }
    }
    rmdir($path);
}

It works, because RII::CHILD_FIRST iterates over the children before the parent element. So by the time it reaches the directory, it should be empty.

But the actual error is due to where you delete your directories. In inner directories you do it in the parent iteration. That means that your root directory will never be deleted. I'd suggest doing it in the local delete iteration:

public function delete($path) {
    if(!file_exists($path)) {
        throw new RecursiveDirectoryException('Directory doesn\'t exist.');
    }

    $directoryIterator = new DirectoryIterator($path);

    foreach($directoryIterator as $fileInfo) {
        $filePath = $fileInfo->getPathname();
        if(!$fileInfo->isDot()) {
            if($fileInfo->isFile()) {
                unlink($filePath);
            } elseif($fileInfo->isDir()) {
                if($this->emptyDirectory($filePath)) {
                    rmdir($filePath);
                } else {
                    $this->delete($filePath);
                }
            }
        }
    }
    rmdir($path);
}

Note the two changes. We're only deleting the empty directories inside of the iteration. Calling $this->delete() on it will handle the deletion for you. The second change is the addition of the final rmdir at the end of the method...

Upvotes: 12

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

function delete($path){
    if(!file_exists($path)) {
        throw new RecursiveDirectoryException('Directory doesn\'t exist.');
    }

    $directoryIterator = new DirectoryIterator($path);

    foreach($directoryIterator as $fileInfo) {
        $filePath = $fileInfo->getPathname();

        if(!$fileInfo->isDot()) {
            if($fileInfo->isFile()) {
                unlink($filePath);
            }
            else if($fileInfo->isDir()) {
                if($this->emptyDirectory($filePath)) {
                    rmdir($filePath);
                }
                else {
                    $this->delete($filePath);
                    rmdir($filePath);
                }
            }
        }
    }
    rmdir($path);
}

?

Upvotes: -1

Related Questions