0

I used the following line to create a directory on my server:

mkdir("/var/www/cache/$directory", 0700);

Then I copied some files into it using this:

copy($remote, $local);

Now I'm trying to delete the directory and all of its file with this, but it will not work:

$local = "/var/www/cache/$directory";
$removed = unlink($local);

Is there anyway to do this with a one-liner and not a for loop or the such?

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94

1 Answers1

0

You can do this with RecursiveIteratorIterator class in php.

A simple example from here would be

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}

rmdir($dir);

There are many methods described here too..

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26