Created
October 4, 2011 06:36
-
-
Save k-holy/1261023 to your computer and use it in GitHub Desktop.
ディレクトリ内のファイルを再帰的に削除するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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