Emptying a Directory of All Files and Folders

Problem

You want to empty a directory completely of it's contents.

But you want to retain the directory itself.

Solution

Use the File::cleanDirectory() method.

$success = File::cleanDirectory($directory);

The method will return false if the directory doesn't exist. Otherwise it returns true when complete.

Discussion

This is a wrapper on File::deleteDirectory().

It operates just as File::deleteDirectory() does but it always retains the directory instead of deleting it.

See Recursively Deleting a Directory for more details.

comments powered by Disqus