Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created October 4, 2011 06:36
Show Gist options
  • Save k-holy/1261023 to your computer and use it in GitHub Desktop.
Save k-holy/1261023 to your computer and use it in GitHub Desktop.
ディレクトリ内のファイルを再帰的に削除するサンプル
<?php
namespace Holy\Example;
class Util
{
public function removeFiles($dir, $removeDir = false)
{
if (!file_exists($dir)) {
return;
}
if (false !== ($handle = opendir($dir))) {
while (false !== ($filename = readdir($handle))) {
if (strcmp($filename, '.') === 0
|| strcmp($filename, '..') === 0
|| strcmp($filename, '.svn') === 0) {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $filename;
if (is_dir($path)) {
$this->removeFiles($path, true);
} else {
unlink($path);
}
}
closedir($handle);
if ($removeDir) {
rmdir($dir);
}
}
return;
}
}
$util = new Util();
$util->removeFiles(realpath(__DIR__ . '/cache'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment