ファイルとディレクトリを再帰的に削除するためのこのコードがあります。正常に動作しますが、少し問題があります。$path = /var/www/foo/ の場合、foo 内のすべてが削除されますが、foo は削除されません。foo ディレクトリも削除したい。何か案が?
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);
}
}
}
}
}